(这个版本是仅支持低功耗蓝牙协议,测试用模块为正点原子atk-ble02)
1.添加权限,在manifest.xml中添加
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
2.创建类Ble,ctrlCV:
public class Ble {
UUID uuid_ser = UUID.fromString("9ecadc24-0ee5-a9e0-93f3-a3b50100406e");
UUID uuid_ch_wt = UUID.fromString("9ecadc24-0ee5-a9e0-93f3-a3b50200406e");
UUID uuid_ch_rd = UUID.fromString("9ecadc24-0ee5-a9e0-93f3-a3b50300406e");
UUID uuid_descriptor = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice mdevice;
BluetoothGatt gatt;
BluetoothGattCharacteristic characteristic_write;
BluetoothGattCharacteristic characteristic_read;
BluetoothGattDescriptor descriptor;
Handler handler = new Handler();
private String get_str = "null";
BluetoothGattCallback callback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if(newState == BluetoothProfile.STATE_CONNECTED){
gatt.discoverServices();
Log.e("--","connect success");
}
else{
Log.e("--","connect failed");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> list_ser = gatt.getServices();
for(BluetoothGattService service : list_ser){
Log.e("ser--",service.getUuid().toString());
List<BluetoothGattCharacteristic> list_ch = service.getCharacteristics();
for(BluetoothGattCharacteristic characteristic : list_ch){
Log.e("chara-----",characteristic.getUuid().toString());
if(uuid_ch_rd.equals(characteristic.getUuid())){
characteristic_read = characteristic;
Log.e("--","found rd!");
}
if(uuid_ch_wt.equals(characteristic.getUuid())){
characteristic_write = characteristic;
Log.e("--","found wt!");
}
}
}
try{
gatt.setCharacteristicNotification(characteristic_read, true);
descriptor = characteristic_read.getDescriptor(uuid_descriptor);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
catch (SecurityException e){
Log.e("--","error");
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.d("--","成功写入");
handler.post(new Runnable() {
@Override
public void run() {
Log.e("--","连接成功");
}
});
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.e("!!!","changed!");
byte[] data = characteristic.getValue();
Log.e("!!!",new String(data));
get_str = new String(data);
}
};
public void connect(Context context, String device_name){
for(BluetoothDevice device : adapter.getBondedDevices()){
if(device_name.equals(device.getName())){
mdevice = device;
}
break;
}
gatt = mdevice.connectGatt(context, false, callback);
gatt.connect();
}
public void send_msg(String msg){
characteristic_write.setValue(msg);
gatt.writeCharacteristic(characteristic_write);
}
public String get_msg(){
return get_str;
}
}
3.直接使用方法即可