|
|
发表于 2015-4-15 13:03:28
|
显示全部楼层
从原作者网页信息看需要蓝牙4.0的模块才能支持,HC-05才蓝牙2.0
The current app is setup to work with DFRobot Bluno boards. It should work with any of them (there are a bunch of different ones...). The app connects using the Bluno's Bluetooth LE GATT profile
我反编译了一下其中一个Android的apk,从释出的源代码看确实是用BLE协议,而且会抓固定uuid的设备- package com.shatalmic.unityandroidbluetoothlelib;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothAdapter.LeScanCallback;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothGatt;
- import android.bluetooth.BluetoothGattCallback;
- import android.bluetooth.BluetoothGattCharacteristic;
- import android.bluetooth.BluetoothGattDescriptor;
- import android.bluetooth.BluetoothGattService;
- import android.bluetooth.BluetoothManager;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.pm.PackageManager;
- import android.os.Parcelable;
- import android.util.Base64;
- import android.util.Log;
- import com.unity3d.player.UnityPlayer;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.UUID;
- import java.util.concurrent.atomic.AtomicBoolean;
- public class UnityBluetoothLE
- {
- protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
- public static final String TAG = "UnityBluetoothLE";
- public static UnityBluetoothLE _instance;
- private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver()
- {
- public void onReceive(Context paramContext, Intent paramIntent)
- {
- BluetoothDevice localBluetoothDevice;
- Parcelable[] arrayOfParcelable;
- int i;
- if ("android.bluetooth.device.action.UUID".equals(paramIntent.getAction()))
- {
- UnityBluetoothLE.this.androidBluetoothLog("got action_uuid");
- if (UnityBluetoothLE.this.uuidList != null)
- {
- localBluetoothDevice = (BluetoothDevice)paramIntent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
- arrayOfParcelable = paramIntent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
- if ((localBluetoothDevice == null) || (arrayOfParcelable == null))
- break label144;
- i = arrayOfParcelable.length;
- }
- }
- for (int j = 0; ; j++)
- {
- if (j >= i)
- return;
- String str = arrayOfParcelable[j].toString();
- UnityBluetoothLE.this.androidBluetoothLog("checking uuid " + str);
- if (!UnityBluetoothLE.this.uuidList.contains(UnityBluetoothLE.this.getFullBluetoothLEUUID(str)))
- continue;
- UnityBluetoothLE.this.sendDiscoveredDevice(localBluetoothDevice);
- }
- label144: if (localBluetoothDevice != null)
- UnityBluetoothLE.this.androidBluetoothLog("device: " + localBluetoothDevice.getAddress());
- while (arrayOfParcelable != null)
- {
- UnityBluetoothLE.this.androidBluetoothLog("uuid count: " + arrayOfParcelable.length);
- return;
- UnityBluetoothLE.this.androidBluetoothLog("device is null");
- }
- UnityBluetoothLE.this.androidBluetoothLog("uuidExtra is null");
- }
- };
- private Map<String, BluetoothGatt> deviceGattMap = null;
- private Map<String, BluetoothDevice> deviceMap = null;
- private BluetoothAdapter mBluetoothAdapter;
- private Context mContext = null;
- private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
- {
- public void onCharacteristicChanged(BluetoothGatt paramBluetoothGatt, BluetoothGattCharacteristic paramBluetoothGattCharacteristic)
- {
- byte[] arrayOfByte = paramBluetoothGattCharacteristic.getValue();
- if (arrayOfByte != null)
- {
- String str = Base64.encodeToString(arrayOfByte, 0);
- UnityBluetoothLE.UnitySend("DidUpdateValueForCharacteristic~" + paramBluetoothGattCharacteristic.getUuid() + "~" + str);
- }
- }
- public void onCharacteristicRead(BluetoothGatt paramBluetoothGatt, BluetoothGattCharacteristic paramBluetoothGattCharacteristic, int paramInt)
- {
- if (paramInt == 0)
- {
- byte[] arrayOfByte = paramBluetoothGattCharacteristic.getValue();
- if (arrayOfByte != null)
- {
- String str = Base64.encodeToString(arrayOfByte, 0);
- UnityBluetoothLE.UnitySend("DidUpdateValueForCharacteristic~" + paramBluetoothGattCharacteristic.getUuid() + "~" + str);
- }
- }
- }
- public void onCharacteristicWrite(BluetoothGatt paramBluetoothGatt, BluetoothGattCharacteristic paramBluetoothGattCharacteristic, int paramInt)
- {
- if (paramInt == 0)
- {
- UnityBluetoothLE.UnitySend("DidWriteCharacteristic~" + paramBluetoothGattCharacteristic.getUuid());
- return;
- }
- UnityBluetoothLE.UnitySend("Error~Response - failed to write characteristic: " + paramInt);
- }
- public void onConnectionStateChange(BluetoothGatt paramBluetoothGatt, int paramInt1, int paramInt2)
- {
- UnityBluetoothLE.this.androidBluetoothLog("onConnectionStateChange");
- String str = paramBluetoothGatt.getDevice().getAddress();
- if (paramInt2 == 2)
- {
- if (UnityBluetoothLE.this.deviceGattMap == null)
- UnityBluetoothLE.this.deviceGattMap = new HashMap();
- UnityBluetoothLE.this.deviceGattMap.put(str, paramBluetoothGatt);
- UnityBluetoothLE.UnitySend("ConnectedPeripheral~" + str);
- paramBluetoothGatt.discoverServices();
- }
- do
- return;
- while (paramInt2 != 0);
- UnityBluetoothLE.UnitySend("DisconnectedPeripheral~" + str);
- }
- public void onDescriptorWrite(BluetoothGatt paramBluetoothGatt, BluetoothGattDescriptor paramBluetoothGattDescriptor, int paramInt)
- {
- if (paramInt == 0)
- {
- UnityBluetoothLE.this.androidBluetoothLog("onDescriptorWrite Success");
- if ((paramBluetoothGattDescriptor != null) && (paramBluetoothGattDescriptor.getCharacteristic() != null) && (paramBluetoothGattDescriptor.getCharacteristic().getUuid() != null))
- {
- UnityBluetoothLE.UnitySend("DidUpdateNotificationStateForCharacteristic~" + paramBluetoothGattDescriptor.getCharacteristic().getUuid());
- return;
- }
- UnityBluetoothLE.UnitySend("Error~Descriptor Write Failed: characterstic or uuid blank");
- return;
- }
- UnityBluetoothLE.UnitySend("Error~Descriptor Write Failed: " + paramInt);
- }
- public void onServicesDiscovered(BluetoothGatt paramBluetoothGatt, int paramInt)
- {
- if (paramInt == 0)
- {
- UnityBluetoothLE.this.androidBluetoothLog("Services Discovered");
- List localList = paramBluetoothGatt.getServices();
- String str;
- Iterator localIterator1;
- if (localList != null)
- {
- str = paramBluetoothGatt.getDevice().getAddress();
- localIterator1 = localList.iterator();
- }
- while (true)
- {
- if (!localIterator1.hasNext())
- return;
- BluetoothGattService localBluetoothGattService = (BluetoothGattService)localIterator1.next();
- UnityBluetoothLE.UnitySend("DiscoveredService~" + str + "~" + localBluetoothGattService.getUuid());
- Iterator localIterator2 = localBluetoothGattService.getCharacteristics().iterator();
- while (localIterator2.hasNext())
- {
- BluetoothGattCharacteristic localBluetoothGattCharacteristic = (BluetoothGattCharacteristic)localIterator2.next();
- UnityBluetoothLE.UnitySend("DiscoveredCharacteristic~" + str + "~" + localBluetoothGattService.getUuid() + "~" + localBluetoothGattCharacteristic.getUuid());
- }
- }
- }
- UnityBluetoothLE.this.androidBluetoothLog("Error~Service Discovery " + paramInt);
- }
- };
- private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback()
- {
- public void onLeScan(BluetoothDevice paramBluetoothDevice, int paramInt, byte[] paramArrayOfByte)
- {
- UnityBluetoothLE.this.sendDiscoveredDevice(paramBluetoothDevice);
- }
- };
- public AtomicBoolean mRunning = new AtomicBoolean();
- private ArrayList<UUID> uuidList = null;
- public static void UnitySend(String paramString)
- {
- UnityPlayer.UnitySendMessage("BluetoothLEReceiver", "OnBluetoothMessage", paramString);
- }
- public static void UnitySend(byte[] paramArrayOfByte, int paramInt)
- {
- UnityPlayer.UnitySendMessage("BluetoothLEReceiver", "OnBluetoothData", Base64.encodeToString(Arrays.copyOfRange(paramArrayOfByte, 0, paramInt), 0));
- }
- private UUID getFullBluetoothLEUUID(String paramString)
- {
- if (paramString.length() == 4)
- return UUID.fromString("0000" + paramString + "-0000-1000-8000-00805F9B34FB");
- return UUID.fromString(paramString);
- }
- public static UnityBluetoothLE getInstance()
- {
- if (_instance == null)
- _instance = new UnityBluetoothLE();
- return _instance;
- }
- private void sendDiscoveredDevice(BluetoothDevice paramBluetoothDevice)
- {
- String str = "No Name";
- if (paramBluetoothDevice.getName() != null)
- str = paramBluetoothDevice.getName();
- if (this.deviceMap == null)
- this.deviceMap = new HashMap();
- this.deviceMap.put(paramBluetoothDevice.getAddress(), paramBluetoothDevice);
- UnitySend("DiscoveredPeripheral~" + paramBluetoothDevice.getAddress() + "~" + str);
- }
- public void androidBluetoothConnectToPeripheral(String paramString)
- {
- if ((this.mBluetoothAdapter != null) && (this.deviceMap != null) && (this.mContext != null))
- {
- BluetoothDevice localBluetoothDevice = (BluetoothDevice)this.deviceMap.get(paramString);
- if (localBluetoothDevice != null)
- UnityPlayer.currentActivity.runOnUiThread(new Runnable(localBluetoothDevice)
- {
- public void run()
- {
- this.val$device.connectGatt(UnityBluetoothLE.this.mContext, false, UnityBluetoothLE.this.mGattCallback);
- }
- });
- }
- }
- public void androidBluetoothDeInitialize()
- {
- Log.d("UnityBluetoothLE", "androidBluetoothDeInitialize");
- onDestroy();
- UnitySend("DeInitialized");
- }
- public void androidBluetoothDisconnectPeripheral(String paramString)
- {
- if ((this.mBluetoothAdapter != null) && (this.deviceGattMap != null) && (this.mContext != null))
- {
- BluetoothGatt localBluetoothGatt = (BluetoothGatt)this.deviceGattMap.get(paramString);
- if (localBluetoothGatt != null)
- localBluetoothGatt.disconnect();
- }
- }
- public void androidBluetoothInitialize(boolean paramBoolean1, boolean paramBoolean2)
- {
- Log.d("UnityBluetoothLE", "androidBluetoothInitialize");
- this.mContext = UnityPlayer.currentActivity.getApplicationContext();
- if (!this.mContext.getPackageManager().hasSystemFeature("android.hardware.bluetooth_le"))
- {
- UnitySend("Error~Bluetooth Low Energy Not Available");
- return;
- }
- this.uuidList = null;
- this.deviceMap = null;
- this.deviceGattMap = null;
- IntentFilter localIntentFilter = new IntentFilter("android.bluetooth.device.action.FOUND");
- localIntentFilter.addAction("android.bluetooth.device.action.UUID");
- localIntentFilter.addAction("android.bluetooth.adapter.action.DISCOVERY_STARTED");
- localIntentFilter.addAction("android.bluetooth.adapter.action.DISCOVERY_FINISHED");
- this.mContext.registerReceiver(this.ActionFoundReceiver, localIntentFilter);
- this.mBluetoothAdapter = ((BluetoothManager)this.mContext.getSystemService("bluetooth")).getAdapter();
- UnitySend("Initialized");
- }
- public void androidBluetoothLog(String paramString)
- {
- Log.i("UnityBluetoothLE", paramString);
- }
- public void androidBluetoothPause(boolean paramBoolean)
- {
- }
- public void androidBluetoothRetrieveListOfPeripheralsWithServices(String paramString)
- {
- String[] arrayOfString;
- int i;
- if (this.mBluetoothAdapter != null)
- {
- this.uuidList = new ArrayList();
- if (!paramString.contains("|"))
- break label99;
- arrayOfString = paramString.split("|");
- if ((arrayOfString != null) && (arrayOfString.length > 0))
- {
- i = 0;
- if (i < arrayOfString.length)
- break label77;
- }
- }
- while (true)
- {
- androidBluetoothLog("getting bonded devices");
- UnityPlayer.currentActivity.runOnUiThread(new Runnable()
- {
- public void run()
- {
- Set localSet = UnityBluetoothLE.this.mBluetoothAdapter.getBondedDevices();
- Iterator localIterator;
- if ((localSet != null) && (localSet.size() > 0))
- localIterator = localSet.iterator();
- while (true)
- {
- if (!localIterator.hasNext())
- return;
- BluetoothDevice localBluetoothDevice = (BluetoothDevice)localIterator.next();
- if (localBluetoothDevice == null)
- continue;
- UnityBluetoothLE.this.androidBluetoothLog("got device " + localBluetoothDevice.getAddress());
- if (UnityBluetoothLE.this.uuidList.size() > 0)
- {
- localBluetoothDevice.fetchUuidsWithSdp();
- continue;
- }
- UnityBluetoothLE.this.sendDiscoveredDevice(localBluetoothDevice);
- }
- }
- });
- return;
- label77: this.uuidList.add(getFullBluetoothLEUUID(arrayOfString[i]));
- i++;
- break;
- label99: if (paramString.length() <= 0)
- continue;
- this.uuidList.add(getFullBluetoothLEUUID(paramString));
- }
- }
- public void androidBluetoothScanForPeripheralsWithServices(String paramString)
- {
- ArrayList localArrayList;
- String[] arrayOfString;
- int i;
- if (this.mBluetoothAdapter != null)
- {
- localArrayList = new ArrayList();
- if (paramString != null)
- {
- if (!paramString.contains("|"))
- break label118;
- arrayOfString = paramString.split("|");
- if ((arrayOfString != null) && (arrayOfString.length > 0))
- {
- i = 0;
- if (i < arrayOfString.length)
- break label98;
- }
- }
- }
- while (true)
- {
- if (localArrayList.size() <= 0)
- break label138;
- UUID[] arrayOfUUID = (UUID[])localArrayList.toArray(new UUID[localArrayList.size()]);
- this.mBluetoothAdapter.startLeScan(arrayOfUUID, this.mLeScanCallback);
- return;
- label98: localArrayList.add(getFullBluetoothLEUUID(arrayOfString[i]));
- i++;
- break;
- label118: if (paramString.length() <= 0)
- continue;
- localArrayList.add(getFullBluetoothLEUUID(paramString));
- }
- label138: this.mBluetoothAdapter.startLeScan(this.mLeScanCallback);
- }
- public void androidBluetoothStopScan()
- {
- if (this.mBluetoothAdapter != null)
- this.mBluetoothAdapter.stopLeScan(this.mLeScanCallback);
- }
- public void androidReadCharacteristic(String paramString1, String paramString2, String paramString3)
- {
- if ((this.mBluetoothAdapter != null) && (this.deviceGattMap != null) && (this.mContext != null))
- {
- BluetoothGatt localBluetoothGatt = (BluetoothGatt)this.deviceGattMap.get(paramString1);
- if (localBluetoothGatt != null)
- {
- UUID localUUID1 = getUUID(paramString2);
- if (localUUID1 == null)
- break label131;
- BluetoothGattService localBluetoothGattService = localBluetoothGatt.getService(localUUID1);
- if (localBluetoothGattService == null)
- break label124;
- UUID localUUID2 = getUUID(paramString3);
- if (localUUID2 == null)
- break label117;
- BluetoothGattCharacteristic localBluetoothGattCharacteristic = localBluetoothGattService.getCharacteristic(localUUID2);
- if (localBluetoothGattCharacteristic == null)
- break label110;
- if (!localBluetoothGatt.readCharacteristic(localBluetoothGattCharacteristic))
- UnitySend("Error~Failed to read characteristic");
- }
- }
- return;
- label110: UnitySend("Error~Characteristic not found for Read");
- return;
- label117: UnitySend("Error~Not a Valid Characteristic UUID for Read");
- return;
- label124: UnitySend("Error~Service not found for Read");
- return;
- label131: UnitySend("Error~Not a Valid Service UUID for Read");
- }
- protected void androidSetCharacteristicNotification(String paramString1, String paramString2, String paramString3, boolean paramBoolean)
- {
- androidBluetoothLog("1");
- BluetoothGatt localBluetoothGatt;
- BluetoothGattDescriptor localBluetoothGattDescriptor;
- if ((this.mBluetoothAdapter != null) && (this.deviceGattMap != null) && (this.mContext != null))
- {
- androidBluetoothLog("2");
- localBluetoothGatt = (BluetoothGatt)this.deviceGattMap.get(paramString1);
- if (localBluetoothGatt != null)
- {
- androidBluetoothLog("3");
- UUID localUUID1 = getUUID(paramString2);
- if (localUUID1 == null)
- break label268;
- androidBluetoothLog("4");
- BluetoothGattService localBluetoothGattService = localBluetoothGatt.getService(localUUID1);
- if (localBluetoothGattService == null)
- break label261;
- androidBluetoothLog("5");
- UUID localUUID2 = getUUID(paramString3);
- if (localUUID2 == null)
- break label254;
- androidBluetoothLog("6");
- BluetoothGattCharacteristic localBluetoothGattCharacteristic = localBluetoothGattService.getCharacteristic(localUUID2);
- if (localBluetoothGattCharacteristic == null)
- break label247;
- androidBluetoothLog("7");
- if (!localBluetoothGatt.setCharacteristicNotification(localBluetoothGattCharacteristic, true))
- break label240;
- androidBluetoothLog("8");
- localBluetoothGattDescriptor = localBluetoothGattCharacteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
- if (localBluetoothGattDescriptor == null)
- break label233;
- androidBluetoothLog("9");
- if (!paramBoolean)
- break label217;
- }
- }
- label217: for (byte[] arrayOfByte = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE; ; arrayOfByte = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)
- {
- localBluetoothGattDescriptor.setValue(arrayOfByte);
- if (localBluetoothGatt.writeDescriptor(localBluetoothGattDescriptor))
- break;
- UnitySend("Error~Failed to write characteristic descriptor");
- return;
- }
- androidBluetoothLog("10");
- return;
- label233: UnitySend("Error~Failed to get notification descriptor");
- return;
- label240: UnitySend("Error~Failed to set characteristic notification");
- return;
- label247: UnitySend("Error~Characteristic not found for Subscribe");
- return;
- label254: UnitySend("Error~Not a Valid Characteristic UUID for Subscribe");
- return;
- label261: UnitySend("Error~Service not found for Subscribe");
- return;
- label268: UnitySend("Error~Not a Valid Service UUID for Subscribe");
- }
- public void androidSubscribeCharacteristic(String paramString1, String paramString2, String paramString3)
- {
- androidBluetoothLog("subscribe characteristic");
- androidSetCharacteristicNotification(paramString1, paramString2, paramString3, true);
- }
- public void androidUnsubscribeCharacteristic(String paramString1, String paramString2, String paramString3)
- {
- androidBluetoothLog("unsubscribe characteristic");
- androidSetCharacteristicNotification(paramString1, paramString2, paramString3, false);
- }
- public void androidWriteCharacteristic(String paramString1, String paramString2, String paramString3, byte[] paramArrayOfByte, int paramInt, boolean paramBoolean)
- {
- BluetoothGatt localBluetoothGatt;
- BluetoothGattCharacteristic localBluetoothGattCharacteristic;
- if ((this.mBluetoothAdapter != null) && (this.deviceGattMap != null) && (this.mContext != null))
- {
- localBluetoothGatt = (BluetoothGatt)this.deviceGattMap.get(paramString1);
- if (localBluetoothGatt != null)
- {
- UUID localUUID1 = getUUID(paramString2);
- if (localUUID1 == null)
- break label167;
- BluetoothGattService localBluetoothGattService = localBluetoothGatt.getService(localUUID1);
- if (localBluetoothGattService == null)
- break label160;
- UUID localUUID2 = getUUID(paramString3);
- if (localUUID2 == null)
- break label153;
- localBluetoothGattCharacteristic = localBluetoothGattService.getCharacteristic(localUUID2);
- if (localBluetoothGattCharacteristic == null)
- break label146;
- androidBluetoothLog("write characteristic");
- localBluetoothGattCharacteristic.setValue(paramArrayOfByte);
- if (!paramBoolean)
- break label140;
- }
- }
- label140: for (int i = 2; ; i = 1)
- {
- localBluetoothGattCharacteristic.setWriteType(i);
- if (!localBluetoothGatt.writeCharacteristic(localBluetoothGattCharacteristic))
- UnitySend("Error~Failed to write characteristic");
- return;
- }
- label146: UnitySend("Error~Characteristic not found for Write");
- return;
- label153: UnitySend("Error~Not a Valid Characteristic UUID for Write");
- return;
- label160: UnitySend("Error~Service not found for Write");
- return;
- label167: UnitySend("Error~Not a Valid Service UUID for Write");
- }
- protected UUID getUUID(String paramString)
- {
- UUID localUUID;
- if (paramString.length() == 36)
- localUUID = UUID.fromString(paramString);
- int i;
- do
- {
- return localUUID;
- i = paramString.length();
- localUUID = null;
- }
- while (i > 8);
- StringBuilder localStringBuilder = new StringBuilder();
- localStringBuilder.append("00000000", 0, 8 - paramString.length());
- localStringBuilder.append(paramString);
- localStringBuilder.append("-0000-1000-8000-00805f9b34fb");
- return UUID.fromString(localStringBuilder.toString());
- }
- public void onDestroy()
- {
- if (this.mContext != null)
- {
- this.mContext.unregisterReceiver(this.ActionFoundReceiver);
- this.uuidList = null;
- this.deviceMap = null;
- this.deviceGattMap = null;
- }
- }
- }
复制代码 |
|