分享Android 蓝牙4.0(ble)开发的解决方案

2019-12-10 18:47:32于海丽

2.4.开始搜索设备。

 

复制代码 mBluetoothAdapter.startLeScan(mLeScanCallback); 

 

2.5.BluetoothDevice  描述了一个蓝牙设备 提供了getAddress()设备Mac地址,getName()设备的名称。
2.6开始连接设备

 /** 
  * Connects to the GATT server hosted on the Bluetooth LE device. 
  * 
  * @param address 
  *   The device address of the destination device. 
  * 
  * @return Return true if the connection is initiated successfully. The 
  *   connection result is reported asynchronously through the 
  *   {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} 
  *   callback. 
  */ 
 public boolean connect(final String address) { 
  if (mBluetoothAdapter == null || address == null) { 
   Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); 
   return false; 
  } 
 
  // Previously connected device. Try to reconnect. (先前连接的设备。 尝试重新连接) 
  if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { 
   Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); 
   if (mBluetoothGatt.connect()) { 
    mConnectionState = STATE_CONNECTING; 
    return true; 
   } else { 
    return false; 
   } 
  } 
 
  final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
  if (device == null) { 
   Log.w(TAG, "Device not found. Unable to connect."); 
   return false; 
  } 
  // We want to directly connect to the device, so we are setting the 
  // autoConnect 
  // parameter to false. 
  mBluetoothGatt = device.connectGatt(this, false, mGattCallback); 
  Log.d(TAG, "Trying to create a new connection."); 
  mBluetoothDeviceAddress = address; 
  mConnectionState = STATE_CONNECTING; 
  return true; 
 } 

2.7连接到设备之后获取设备的服务(Service)和服务对应的Characteristic。

// Demonstrates how to iterate through the supported GATT 
// Services/Characteristics. 
// In this sample, we populate the data structure that is bound to the 
// ExpandableListView 
// on the UI. 
private void displayGattServices(List<BluetoothGattService> gattServices) { 
 if (gattServices == null) 
  return; 
 String uuid = null; 
 ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<>(); 
 ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<>(); 
 
 mGattCharacteristics = new ArrayList<>(); 
 
 // Loops through available GATT Services. 
 for (BluetoothGattService gattService : gattServices) { 
  HashMap<String, String> currentServiceData = new HashMap<>(); 
  uuid = gattService.getUuid().toString(); 
  if (uuid.contains("ba11f08c-5f14-0b0d-1080")) {//服务的uuid 
   //System.out.println("this gattService UUID is:" + gattService.getUuid().toString()); 
   currentServiceData.put(LIST_NAME, "Service_OX100"); 
   currentServiceData.put(LIST_UUID, uuid); 
   gattServiceData.add(currentServiceData); 
   ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<>(); 
   List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); 
   ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<>(); 
 
   // Loops through available Characteristics. 
   for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { 
    charas.add(gattCharacteristic); 
    HashMap<String, String> currentCharaData = new HashMap<>(); 
    uuid = gattCharacteristic.getUuid().toString(); 
    if (uuid.toLowerCase().contains("cd01")) { 
     currentCharaData.put(LIST_NAME, "cd01"); 
    } else if (uuid.toLowerCase().contains("cd02")) { 
     currentCharaData.put(LIST_NAME, "cd02"); 
    } else if (uuid.toLowerCase().contains("cd03")) { 
     currentCharaData.put(LIST_NAME, "cd03"); 
    } else if (uuid.toLowerCase().contains("cd04")) { 
     currentCharaData.put(LIST_NAME, "cd04"); 
    } else { 
     currentCharaData.put(LIST_NAME, "write"); 
    } 
 
    currentCharaData.put(LIST_UUID, uuid); 
    gattCharacteristicGroupData.add(currentCharaData); 
   } 
 
   mGattCharacteristics.add(charas); 
 
   gattCharacteristicData.add(gattCharacteristicGroupData); 
 
   mCharacteristicCD01 = gattService.getCharacteristic(UUID.fromString("0000cd01-0000-1000-8000-00805f9b34fb")); 
   mCharacteristicCD02 = gattService.getCharacteristic(UUID.fromString("0000cd02-0000-1000-8000-00805f9b34fb")); 
   mCharacteristicCD03 = gattService.getCharacteristic(UUID.fromString("0000cd03-0000-1000-8000-00805f9b34fb")); 
   mCharacteristicCD04 = gattService.getCharacteristic(UUID.fromString("0000cd04-0000-1000-8000-00805f9b34fb")); 
   mCharacteristicWrite = gattService.getCharacteristic(UUID.fromString("0000cd20-0000-1000-8000-00805f9b34fb")); 
 
   //System.out.println("=======================Set Notification=========================="); 
   // 开始顺序监听,第一个:CD01 
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD01, true); 
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD02, true); 
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD03, true); 
   mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD04, true); 
  } 
 } 
}