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

+14 -8
View File
@@ -245,12 +245,12 @@ public class BluetoothDeviceScript : MonoBehaviour
else if (message.Length >= deviceDiscoveredService.Length && message.Substring (0, deviceDiscoveredService.Length) == deviceDiscoveredService)
{
if (parts.Length >= 3 && DiscoveredServiceAction != null)
DiscoveredServiceAction (parts[1], parts[2]);
DiscoveredServiceAction (parts[1], FullUUID(parts[2]));
}
else if (message.Length >= deviceDiscoveredCharacteristic.Length && message.Substring (0, deviceDiscoveredCharacteristic.Length) == deviceDiscoveredCharacteristic)
{
if (parts.Length >= 4 && DiscoveredCharacteristicAction != null)
DiscoveredCharacteristicAction (parts[1], parts[2], parts[3]);
DiscoveredCharacteristicAction (parts[1], FullUUID(parts[2]), FullUUID(parts[3]));
}
else if (message.Length >= deviceDidWriteCharacteristic.Length && message.Substring (0, deviceDidWriteCharacteristic.Length) == deviceDidWriteCharacteristic)
{
@@ -264,11 +264,11 @@ public class BluetoothDeviceScript : MonoBehaviour
if (DidUpdateNotificationStateForCharacteristicAction != null && DidUpdateNotificationStateForCharacteristicAction.ContainsKey (parts[1]))
{
var characteristicAction = DidUpdateNotificationStateForCharacteristicAction[parts[1]];
if (characteristicAction != null && characteristicAction.ContainsKey (parts[2]))
if (characteristicAction != null && characteristicAction.ContainsKey (FullUUID(parts[2])))
{
var action = characteristicAction[parts[2]];
var action = characteristicAction[FullUUID(parts[2])];
if (action != null)
action (parts[2]);
action (FullUUID(parts[2]));
}
}
@@ -277,9 +277,9 @@ public class BluetoothDeviceScript : MonoBehaviour
var characteristicAction = DidUpdateNotificationStateForCharacteristicWithDeviceAddressAction[parts[1]];
if (characteristicAction != null && characteristicAction.ContainsKey (parts[2]))
{
var action = characteristicAction[parts[2]];
var action = characteristicAction[FullUUID(parts[2])];
if (action != null)
action (parts[1], parts[2]);
action (parts[1], FullUUID(parts[2]));
}
}
}
@@ -287,7 +287,7 @@ public class BluetoothDeviceScript : MonoBehaviour
else if (message.Length >= deviceDidUpdateValueForCharacteristic.Length && message.Substring (0, deviceDidUpdateValueForCharacteristic.Length) == deviceDidUpdateValueForCharacteristic)
{
if (parts.Length >= 4)
OnBluetoothData (parts[1], parts[2], parts[3]);
OnBluetoothData (parts[1], FullUUID(parts[2]), parts[3]);
}
else if (message.Length >= deviceRequestMtu.Length && message.Substring(0, deviceRequestMtu.Length) == deviceRequestMtu)
{
@@ -303,6 +303,12 @@ public class BluetoothDeviceScript : MonoBehaviour
}
}
}
public static string FullUUID(string uuid)
{
if (uuid.Length == 4)
return "0000" + uuid + "-0000-1000-8000-00805F9B34FB";
return uuid;
}
public void OnBluetoothData (string base64Data)
{
+2 -1
View File
@@ -616,7 +616,7 @@ public class BluetoothLEHardwareInterface
bluetoothDeviceScript.ConnectedPeripheralAction = connectAction;
bluetoothDeviceScript.DiscoveredServiceAction = serviceAction;
bluetoothDeviceScript.DiscoveredCharacteristicAction = characteristicAction;
//bluetoothDeviceScript.ConnectedDisconnectPeripheralAction = disconnectAction;
bluetoothDeviceScript.ConnectedDisconnectPeripheralAction = disconnectAction;
bluetoothDeviceScript.DeviceBleStatusDisconnectedAction = disconnectAction;
}
@@ -1000,3 +1000,4 @@ public class BluetoothLEHardwareInterface
return uuid;
}
}
+14 -8
View File
@@ -816,10 +816,16 @@ extern "C" {
NSString *message = nil;
if (advertisementData != nil && [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey] != nil)
if (advertisementData != nil && [advertisementData objectForKey:@"kCBAdvDataServiceUUIDs"] != nil)
{
NSData* bytes = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
message = [NSString stringWithFormat:@"DiscoveredPeripheral~%@~%@~%@~%@", identifier, name, RSSI, [UnityBluetoothLE base64StringFromData:bytes length:bytes.length]];
NSArray* bytes = [advertisementData objectForKey:@"kCBAdvDataServiceUUIDs"];
NSString* list = @"";
for (CBUUID* sUUID in bytes)
{
list = [list stringByAppendingString: sUUID.UUIDString.length == 4? [NSString stringWithFormat:@"0000%@-0000-1000-8000-00805F9B34FB",sUUID.UUIDString]:sUUID.UUIDString];
list = [list stringByAppendingString: @","];
}
message = [NSString stringWithFormat:@"DiscoveredPeripheral~%@~%@~%@~%@", identifier, name, RSSI, list];
}
else if (RSSI != 0 && _rssiOnly)
{
@@ -916,7 +922,7 @@ extern "C" {
{
for (CBService *service in peripheral.services)
{
NSString *message = [NSString stringWithFormat:@"DiscoveredService~%@~%@", foundPeripheral, [service UUID]];
NSString *message = [NSString stringWithFormat:@"DiscoveredService~%@~%@", foundPeripheral,service.UUID.UUIDString];
UnitySendMessage ("BluetoothLEReceiver", "OnBluetoothMessage", [message UTF8String]);
[peripheral discoverCharacteristics:nil forService:service];
@@ -939,7 +945,7 @@ extern "C" {
{
for (CBCharacteristic *characteristic in service.characteristics)
{
NSString *message = [NSString stringWithFormat:@"DiscoveredCharacteristic~%@~%@~%@", foundPeripheral, [service UUID], [characteristic UUID]];
NSString *message = [NSString stringWithFormat:@"DiscoveredCharacteristic~%@~%@~%@", foundPeripheral,service.UUID.UUIDString,characteristic.UUID.UUIDString];
UnitySendMessage ("BluetoothLEReceiver", "OnBluetoothMessage", [message UTF8String]);
}
}
@@ -960,7 +966,7 @@ extern "C" {
{
if (characteristic.value != nil)
{
NSString *message = [NSString stringWithFormat:@"DidUpdateValueForCharacteristic~%@~%@~%@", foundPeripheral, [characteristic UUID], [UnityBluetoothLE base64StringFromData:characteristic.value length:characteristic.value.length]];
NSString *message = [NSString stringWithFormat:@"DidUpdateValueForCharacteristic~%@~%@~%@", foundPeripheral, characteristic.UUID.UUIDString, [UnityBluetoothLE base64StringFromData:characteristic.value length:(int)characteristic.value.length]];
UnitySendMessage ("BluetoothLEReceiver", "OnBluetoothMessage", [message UTF8String]);
//NSString *message = [UnityBluetoothLE base64StringFromData:characteristic.value length:characteristic.value.length];
//UnitySendMessage ("BluetoothLEReceiver", "OnBluetoothData", [message UTF8String] );
@@ -1072,7 +1078,7 @@ extern "C" {
NSString *foundPeripheral = [self findPeripheralName:peripheral];
if (foundPeripheral != nil)
{
NSString *message = [NSString stringWithFormat:@"DidUpdateNotificationStateForCharacteristic~%@~%@", foundPeripheral, characteristic.UUID];
NSString *message = [NSString stringWithFormat:@"DidUpdateNotificationStateForCharacteristic~%@~%@", foundPeripheral, characteristic.UUID.UUIDString];
UnitySendMessage ("BluetoothLEReceiver", "OnBluetoothMessage", [message UTF8String] );
}
}
@@ -1166,7 +1172,7 @@ extern "C" {
{
characteristic.value = request.value;
NSString *message = [NSString stringWithFormat:@"PeripheralReceivedWriteData~%@~%@", [characteristic UUID], [UnityBluetoothLE base64StringFromData:characteristic.value length:characteristic.value.length]];
NSString *message = [NSString stringWithFormat:@"PeripheralReceivedWriteData~%@~%@", [characteristic UUID], [UnityBluetoothLE base64StringFromData:characteristic.value length:(int)characteristic.value.length]];
UnitySendMessage ("BluetoothLEReceiver", "OnBluetoothMessage", [message UTF8String]);
success = TRUE;