ios蓝牙适配

This commit is contained in:
lishuo committed 2021-08-30 18:28:26 +08:00
1 parent d8927ceca5
commit 99540eb1fd
6 files changed
+84 -41

No files matched your search

@@ -94,7 +94,7 @@ namespace Assets.Scripts.Devices.Ble
{
return;
}
#if !(UNITY_ANDROID || UNITY_IOS)
var device111 = discoveredDevices.Last().Value;
if (device111 != null && device111.State == Ant.DeviceState.Disconnected)
{
@@ -103,10 +103,8 @@ namespace Assets.Scripts.Devices.Ble
//TODO:取消注释,自动连接设备
//Debug.Log("自动连接" + device111.Id);
device111.Connect();
Thread.Sleep(2000);
}
}
#endif
//discoveredDevices.Add(device.Peripheral.Address, new BleDevice(device.Peripheral, hwInterface, device.SensorType));
@@ -124,13 +124,13 @@ namespace Assets.Scripts.Ble
if (uuids != null && uuids.Length > 0)
{
foreach (var item in uuids)
foreach (var item1 in uuids)
{
services.Add(new Guid(item));
services.Add(new Guid(item1));
}
foreach (var item in ServiceUuids.Services)
{
if (!uuids.Contains(item.IdGuid.ToString()))
if (!services.Contains(item.IdGuid))
{
continue;
}
@@ -207,26 +207,41 @@ namespace Assets.Scripts.Ble
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
{
BleResponse s = new BleResponse();
s.IsSuccess = true;
s.Error = null;
var self = this;
BluetoothLEHardwareInterface.ConnectToPeripheral(info.Address, (address) =>
if (!this.callbacks.ContainsKey(info))
{
callback?.Invoke(self, info, s);
}, (address, service) => {
ServicesDiscovered(address, service);
}, (address, service, characteristic) =>
this.callbacks.Add(info, callback);
}
if (this.callbacks.Count == 1)
{
CharacteristicsDiscovered(address, service, characteristic);
}, (address)=> {
PeripheralDisconnected(address, info);
});
BleResponse s = new BleResponse();
s.IsSuccess = true;
s.Error = null;
var self = this;
BluetoothLEHardwareInterface.ConnectToPeripheral(info.Address, (address) =>
{
callback?.Invoke(self, info, s);
this.callbacks.Remove(info);
if (this.callbacks.Any())
{
this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value);
}
}, (address, service) =>
{
ServicesDiscovered(address, service);
}, (address, service, characteristic) =>
{
CharacteristicsDiscovered(address, service, characteristic);
}, (address) =>
{
PeripheralDisconnected(address, info);
});
}
}
#region 私有方法
//服务发现
private void ServicesDiscovered(string address, string service)
{
BluetoothLEHardwareInterface.Log(address+"FOUNDFSERVICE:" + service);
if (servicesCallbacks.Where(c => c.Key.Address == address).Any())
{
@@ -245,6 +260,7 @@ namespace Assets.Scripts.Ble
//特征值发现
private void CharacteristicsDiscovered(string address, string service, string characteristic)
{
BluetoothLEHardwareInterface.Log(address + "发现特征值:" + characteristic);
if (characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).Any())
{
var characteristicCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).FirstOrDefault();
@@ -262,12 +278,18 @@ namespace Assets.Scripts.Ble
//设备主动断开处理
private void PeripheralDisconnected(string address, BlePeripheralInfo info)
{
var currentCallback = this.callbacks.Where(c => c.Key.MatchAddress(address));
if (currentCallback.Any())
{
this.callbacks.Remove(currentCallback.First().Key);
}
var characteristicCallback = characteristicNotificationCallbacks.Where(c => c.Key.MatchAddress(address));
if (characteristicCallback.Any())
{
characteristicNotificationCallbacks.Remove(characteristicCallback.First().Key);
}
var serviceCallback = servicesCallbacks.Where(c => c.Key.MatchAddress(address));
if (serviceCallback.Any())
{
@@ -319,17 +341,27 @@ namespace Assets.Scripts.Ble
//发现服务列表
public void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback)
{
this.servicesCallbacks.Add(peripheral, callback);
if (!this.servicesCallbacks.ContainsKey(peripheral))
{
this.servicesCallbacks.Add(peripheral, callback);
}
}
//发现特征值
public void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback)
{
this.characteristicsDiscoveredCallbacks.Add(service, callback);
if (!this.characteristicsDiscoveredCallbacks.ContainsKey(service))
{
this.characteristicsDiscoveredCallbacks.Add(service, callback);
}
}
//订阅特征值
public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> callback)
{
this.characteristicNotificationCallbacks.Add(characteristic.Peripheral, callback);
if (!this.characteristicNotificationCallbacks.ContainsKey(characteristic.Peripheral))
{
this.characteristicNotificationCallbacks.Add(characteristic.Peripheral, callback);
}
BluetoothLEHardwareInterface.Log("SubscribeCharacteristic" + characteristic.Peripheral.Name + characteristic.Service.ToString() + characteristic.Service.ToString());
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(characteristic.Peripheral.Address, characteristic.Service.ToString(), characteristic.Id.ToString(), null, (deviceAddress, characteristric, bytes) =>
{
if (characteristicReadEvent != null)