|
|
@@ -4,7 +4,6 @@ import android.bluetooth.BluetoothAdapter;
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
import android.bluetooth.BluetoothGattService;
|
|
|
-import android.bluetooth.BluetoothManager;
|
|
|
import android.content.BroadcastReceiver;
|
|
|
import android.content.ComponentName;
|
|
|
import android.content.Context;
|
|
|
@@ -14,10 +13,9 @@ import android.content.ServiceConnection;
|
|
|
import android.os.Handler;
|
|
|
import android.os.IBinder;
|
|
|
import android.util.Log;
|
|
|
-import android.widget.Toast;
|
|
|
+import android.widget.TextView;
|
|
|
|
|
|
import com.slam.bboxblelib.BluetoothLeService;
|
|
|
-import com.slam.bboxblelib.CheckboxEvent;
|
|
|
import com.slam.bboxblelib.Conversion;
|
|
|
import com.slam.bboxblelib.DataEvent;
|
|
|
import com.slam.bboxblelib.DeviceEvent;
|
|
|
@@ -26,16 +24,18 @@ import com.slam.bboxblelib.MainBluetooth;
|
|
|
import com.slam.bboxblelib.Responser;
|
|
|
import com.slam.bboxblelib.WriteResponser;
|
|
|
|
|
|
-import org.greenrobot.eventbus.EventBus;
|
|
|
-import org.greenrobot.eventbus.Subscribe;
|
|
|
-import org.greenrobot.eventbus.ThreadMode;
|
|
|
-
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import static android.content.Context.BIND_AUTO_CREATE;
|
|
|
+import static com.slam.bboxblelib.GattAttributes.UUID_FFF_service;
|
|
|
|
|
|
/**
|
|
|
* Created by slambb on 2021/1/14.
|
|
|
@@ -43,6 +43,25 @@ import static android.content.Context.BIND_AUTO_CREATE;
|
|
|
|
|
|
public class MainBluetoothImpl implements MainBluetooth {
|
|
|
private final static String TAG = MainBluetoothImpl.class.getSimpleName();
|
|
|
+
|
|
|
+ //Responser value
|
|
|
+ public static final int UNABLE_INIT_BLUETOOTH = 10000;
|
|
|
+ public static final int DISABLE_BLUETOOTH = 10001;
|
|
|
+ public static final int STATE_CONNECTED = 10002;
|
|
|
+ public static final int UNABLE_CONNECT_BLUETOOTH = 10003;
|
|
|
+ public static final int STOP_SCAN_BLUETOOTH = 10004;
|
|
|
+ public static final int CONNECT_BLUETOOTH = 10005;
|
|
|
+ public static final int BONED_NOT_CONNECTED = 10007;
|
|
|
+ public static final int GATT_CONNECTION = 20001;
|
|
|
+ public static final int GATT_DISCONNECTED = 20002;
|
|
|
+ public static final int GATT_SERVICES_DISCOVERED = 20002;
|
|
|
+ public static final int GATT_SERVICES_NULL = 20003;
|
|
|
+ public static final int GATT_CHARACTERISTIC_NOTIFICATION_SUCCESS = 20004;
|
|
|
+ public static final int GATT_CHARACTERISTIC_NOTIFICATION_CHANGED = 20005;
|
|
|
+ public static final int GATT_CHARACTERISTIC_NOTIFICATION_STRING = 20006;
|
|
|
+ public static final int GATT_CHARACTERISTIC_WRITE_SUCCESS = 20007;
|
|
|
+ public static final int GATT_RESPONSE_FAIL = 20008;
|
|
|
+
|
|
|
// Stops scanning after 10 seconds.
|
|
|
private static final long SCAN_PERIOD = 10000;
|
|
|
private BluetoothLeService mBluetoothLeService;
|
|
|
@@ -50,23 +69,26 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
private String mDeviceName;
|
|
|
private String mDeviceAddress;
|
|
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
- private int mFragmentIndex = 2; //0: query 1: script 2:uuid 3: report 4: bekenTest
|
|
|
private int retryNo = 3;
|
|
|
private Handler mHandler;
|
|
|
private boolean flagDiscoverFail = false;
|
|
|
+ //是否解析数据,默认是开启解析数据
|
|
|
+ private boolean bParsingData = true;
|
|
|
private Context mContext;
|
|
|
+ private BluetoothGattCharacteristic mNotifyCharacteristic;
|
|
|
+ private String searName = "BGBox";
|
|
|
+
|
|
|
//回调
|
|
|
private Responser mResponser;
|
|
|
- private WriteResponser mWriteResponser = null;
|
|
|
// Code to manage Service lifecycle.
|
|
|
private final ServiceConnection mServiceConnection = new ServiceConnection() {
|
|
|
|
|
|
@Override
|
|
|
public void onServiceConnected(ComponentName componentName, IBinder service) {
|
|
|
- mBluetoothLeService = ((BluetoothLeService.LocalBinder)service).getService();
|
|
|
- if (!mBluetoothLeService.initialize(mResponser)) {
|
|
|
+ mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
|
|
|
+ if (!mBluetoothLeService.initialize()) {
|
|
|
Log.e(TAG, "Unable to initialize Bluetooth");
|
|
|
- mResponser.onFailed(10000, "未初始化蓝牙!");
|
|
|
+ mResponser.onFailed(UNABLE_INIT_BLUETOOTH, "未初始化蓝牙!");
|
|
|
}
|
|
|
mConnected = 1;
|
|
|
mBluetoothAdapter = mBluetoothLeService.getmBluetoothAdapter();
|
|
|
@@ -77,7 +99,7 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
@Override
|
|
|
public void onServiceDisconnected(ComponentName componentName) {
|
|
|
Log.e(TAG, "get disconnect event11");
|
|
|
- mBluetoothLeService = null;
|
|
|
+ mBluetoothLeService = null;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -91,23 +113,30 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
final String action = intent.getAction();
|
|
|
- Log.e(TAG, "蓝牙action:" + action);
|
|
|
- if ("android.bluetooth.adapter.action.STATE_CHANGED".equals(action)) {
|
|
|
- switch(mBluetoothAdapter.getState()) {
|
|
|
+ if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
|
|
|
+ switch (mBluetoothAdapter.getState()) {
|
|
|
case 10:
|
|
|
Log.e(TAG, "蓝牙状态:disable");
|
|
|
- mResponser.onFailed(10002, "蓝牙功能禁用!");
|
|
|
+ mResponser.onFailed(DISABLE_BLUETOOTH, "蓝牙功能禁用!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if ("com.example.bluetooth.le.ACTION_GATT_CONNECTED".equals(action)) {
|
|
|
+ if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
|
|
|
Log.e(TAG, "蓝牙状态:ACTION_GATT_CONNECTED");
|
|
|
- if (mBluetoothLeService != null) {
|
|
|
- mBluetoothLeService.testDiscoverService();
|
|
|
+ boolean bDiscover = mBluetoothLeService.discoverService();
|
|
|
+ if (bDiscover) {
|
|
|
+ mResponser.onSuccess(GATT_CONNECTION, "GATT连接成功", null);
|
|
|
+ } else {
|
|
|
+ mResponser.onFailed(GATT_DISCONNECTED, "GATT连接失败");
|
|
|
}
|
|
|
- } else if ("com.example.bluetooth.le.ACTION_GATT_DISCONNECTED".equals(action)) {
|
|
|
+
|
|
|
+ } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
|
|
|
Log.e(TAG, "蓝牙状态:Gatt Disconnected");
|
|
|
- } else if ("com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED".equals(action)) {
|
|
|
+ mResponser.onFailed(GATT_DISCONNECTED, "GATT断开连接。");
|
|
|
+
|
|
|
+ } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
|
|
|
+ mResponser.onSuccess(GATT_SERVICES_DISCOVERED, "GATT服务搜索成功。", null);
|
|
|
+ //已查询到
|
|
|
mConnected = 2;
|
|
|
retryNo = 3;
|
|
|
flagDiscoverFail = false;
|
|
|
@@ -115,44 +144,165 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
List<BluetoothGattService> gattServices = mBluetoothLeService.getSupportedGattServices();
|
|
|
if (gattServices == null) {
|
|
|
Log.e(TAG, "gattService is null");
|
|
|
+ mResponser.onFailed(GATT_SERVICES_NULL, "获取gatt服务列表为空。");
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- Iterator var5 = gattServices.iterator();
|
|
|
-
|
|
|
- while(var5.hasNext()) {
|
|
|
- BluetoothGattService gattService = (BluetoothGattService)var5.next();
|
|
|
- List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
|
|
|
- Iterator var8 = gattCharacteristics.iterator();
|
|
|
-
|
|
|
- while(var8.hasNext()) {
|
|
|
- BluetoothGattCharacteristic gattCharacteristic = (BluetoothGattCharacteristic)var8.next();
|
|
|
- if (gattCharacteristic.getUuid().equals(UUID.fromString(GattAttributes.UUID_FFF_characteristic_notify))) {
|
|
|
- Log.i(TAG, "监听匹配成功:" + GattAttributes.UUID_FFF_characteristic_notify + " ==" + gattCharacteristic.getUuid());
|
|
|
- if (mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true)) {
|
|
|
- mBluetoothLeService.readDescriptor(gattCharacteristic.getDescriptor(UUID.fromString(GattAttributes.uuid_identification_code)));
|
|
|
- }
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
+ BluetoothGattService gattService = mBluetoothLeService.getGattService(UUID.fromString(UUID_FFF_service));
|
|
|
+ BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID.fromString(GattAttributes.UUID_FFF_characteristic_notify));
|
|
|
+
|
|
|
+ final int charaProp = characteristic.getProperties();
|
|
|
+ if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
|
|
|
+ // If there is an active notification on a characteristic, clear
|
|
|
+ // it first so it doesn't update the data field on the user interface.
|
|
|
+ if (mNotifyCharacteristic != null) {
|
|
|
+ mBluetoothLeService.setCharacteristicNotification(
|
|
|
+ mNotifyCharacteristic, false);
|
|
|
+ mNotifyCharacteristic = null;
|
|
|
}
|
|
|
+ mBluetoothLeService.readCharacteristic(characteristic);
|
|
|
}
|
|
|
+ if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
|
|
|
+ mNotifyCharacteristic = characteristic;
|
|
|
+ boolean b1 = mBluetoothLeService.setCharacteristicNotification(
|
|
|
+ characteristic, true);
|
|
|
+ if (b1) {
|
|
|
+ mBluetoothLeService.readDescriptor(characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)));
|
|
|
+ Log.i(TAG, "setCharacteristicNotification1==成功!");
|
|
|
+ mResponser.onSuccess(GATT_CHARACTERISTIC_NOTIFICATION_SUCCESS, "设置GATT通知成功。", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// if (mBluetoothLeService.setCharacteristicNotification(characteristic, true)) {
|
|
|
+// //uuid_identification_code
|
|
|
+// mBluetoothLeService.readDescriptor(characteristic.getDescriptor(UUID.fromString(GattAttributes.uuid_identification_code)));
|
|
|
+// Log.i(TAG, "setCharacteristicNotification1==成功!");
|
|
|
+//
|
|
|
+// }
|
|
|
+// Iterator var5 = gattServices.iterator();
|
|
|
+// while(var5.hasNext()) {
|
|
|
+// BluetoothGattService gattService = (BluetoothGattService)var5.next();
|
|
|
+// BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID.fromString(GattAttributes.UUID_FFF_characteristic_notify));
|
|
|
+//
|
|
|
+// if (mBluetoothLeService.setCharacteristicNotification(characteristic, true)) {
|
|
|
+// //uuid_identification_code
|
|
|
+// mBluetoothLeService.readDescriptor(characteristic.getDescriptor(UUID.fromString(GattAttributes.uuid_identification_code)));
|
|
|
+// Log.i(TAG, "setCharacteristicNotification1==成功!");
|
|
|
+//
|
|
|
+// }
|
|
|
+// List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
|
|
|
+// Log.i(TAG,"11----->" + gattService.getUuid().toString());
|
|
|
+// Iterator var8 = gattCharacteristics.iterator();
|
|
|
+// while(var8.hasNext()) {
|
|
|
+// BluetoothGattCharacteristic gattCharacteristic = (BluetoothGattCharacteristic)var8.next();
|
|
|
+// if (gattCharacteristic.getUuid().equals(UUID.fromString(GattAttributes.UUID_FFF_characteristic_notify))) {
|
|
|
+// Log.i(TAG, "监听匹配成功:" + GattAttributes.UUID_FFF_characteristic_notify + " ==" + gattCharacteristic.getUuid());
|
|
|
+//
|
|
|
+// if (mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true)) {
|
|
|
+// //uuid_identification_code
|
|
|
+// mBluetoothLeService.readDescriptor(gattCharacteristic.getDescriptor(UUID.fromString(GattAttributes.uuid_identification_code)));
|
|
|
+// Log.i(TAG, "setCharacteristicNotification1==成功!");
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ } else if (BluetoothLeService.ACTION_DATA_AVAILABLE_SUCCESS.equals(action)) {
|
|
|
+// Pattern pattern = Pattern.compile("[0]*");
|
|
|
+// Matcher isNum = pattern.matcher(_hexStr);
|
|
|
+// if(isNum.matches()){
|
|
|
+// Log.e(TAG, "返回0不走解析");
|
|
|
+// return;
|
|
|
+//// }
|
|
|
+// String _hexStr;
|
|
|
+// _hexStr = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
|
|
+// Log.i(TAG, "hex:" + _hexStr + Integer.parseInt(_hexStr));
|
|
|
+
|
|
|
+ } else if (BluetoothLeService.ACTION_RESPONSE_SUCCESS.equals(action)) {
|
|
|
+// String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
|
|
+// String code = intent.getStringExtra(BluetoothLeService.EXTRA_CODE);
|
|
|
+// String message = intent.getStringExtra(BluetoothLeService.EXTRA_MESSAGE);
|
|
|
+ //todo 目前只处理写入成功回调
|
|
|
+ mResponser.onSuccess(GATT_CHARACTERISTIC_WRITE_SUCCESS, "设置GATT写入数据成功!", null);
|
|
|
+ } else if (BluetoothLeService.ACTION_RESPONSE_FAIL.equals(action)) {
|
|
|
+// GATT_GET_ERROR(10004,"获取Gatt特服务失败!"),
|
|
|
+// GATT_GET_SERVICE_ERROR(10005,"获取写入服务失败!"),
|
|
|
+// GATT_GET_CHARACTERISTIC_ERROR(10006,"获取写入特征值失败!"),
|
|
|
+// GATT_WRITE_ERROR(10007,"写入数据失败!"),
|
|
|
+ Integer code = intent.getIntExtra(BluetoothLeService.EXTRA_CODE, 0);
|
|
|
+ String message = intent.getStringExtra(BluetoothLeService.EXTRA_MESSAGE);
|
|
|
+ //连接错误
|
|
|
+ mResponser.onFailed(code, message);
|
|
|
} else {
|
|
|
- String a;
|
|
|
- if ("com.example.bluetooth.le.ACTION_DATA_AVAILABLE".equals(action)) {
|
|
|
- a = intent.getStringExtra("com.example.bluetooth.le.EXTRA_DATA");
|
|
|
- if (mWriteResponser != null) {
|
|
|
- DataEvent dataEvent = new DataEvent("hex", a);
|
|
|
- mWriteResponser.onUpdateData(dataEvent);
|
|
|
+ //更新change数据
|
|
|
+ String _hexStr;
|
|
|
+ if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
|
|
|
+ _hexStr = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
|
|
+ if (bParsingData) {
|
|
|
+ //R/L 左手还是右手
|
|
|
+ String _hand = _hexStr.substring(6, 8);
|
|
|
+ String axStr = _hexStr.substring(8, 12);
|
|
|
+ String ayStr = _hexStr.substring(12, 16);
|
|
|
+ String azStr = _hexStr.substring(16, 20);
|
|
|
+ //一个字节最大只能表示265 2个字节可以表示65536 然后你这边做一下转换 例如00 3E 3*16+14=62 ----->0.03 g
|
|
|
+ //(真实值= AD值 /32768 * 16)
|
|
|
+ //0.003 = 62 / 2768 * 16
|
|
|
+ double ax = Conversion.parseHex4(axStr) / 32768 * 16;
|
|
|
+ double ay = Conversion.parseHex4(ayStr) / 32768 * 16;
|
|
|
+ double az = Conversion.parseHex4(azStr) / 32768 * 16;
|
|
|
+ Map<String, Double> _acc = new HashMap<String, Double>();
|
|
|
+ _acc.put("ax", ax);
|
|
|
+ _acc.put("ay", ay);
|
|
|
+ _acc.put("az", az);
|
|
|
+ String gxStr = _hexStr.substring(20, 24);
|
|
|
+ String gyStr = _hexStr.substring(24, 28);
|
|
|
+ String gzStr = _hexStr.substring(28, 32);
|
|
|
+ //角速度(AD值/32768 * 2000) 角速度单位是 °/s 范围是 正负2000
|
|
|
+ double gx = Conversion.parseHex4(gxStr) / 32768 * 2000;
|
|
|
+ double gy = Conversion.parseHex4(gyStr) / 32768 * 2000;
|
|
|
+ double gz = Conversion.parseHex4(gzStr) / 32768 * 2000;
|
|
|
+ Map<String, Double> _gyro = new HashMap<String, Double>();
|
|
|
+ _gyro.put("gx", gx);
|
|
|
+ _gyro.put("gy", gy);
|
|
|
+ _gyro.put("gz", gz);
|
|
|
+ //毫秒
|
|
|
+ Integer ms = Integer.parseInt(_hexStr.substring(2, 6), 16);
|
|
|
+ //分
|
|
|
+ Integer min = Integer.parseInt(_hexStr.substring(32, 34), 16);
|
|
|
+ //秒
|
|
|
+ Integer s = Integer.parseInt(_hexStr.substring(34, 36), 16);
|
|
|
+
|
|
|
+
|
|
|
+ DataEvent dataEvent = new DataEvent(
|
|
|
+ "CHANGED", //dataType
|
|
|
+ _hexStr, //dataStr
|
|
|
+ Conversion.hexStr2Str(_hand), //hand
|
|
|
+ _acc, //加速计
|
|
|
+ _gyro, //陀螺仪
|
|
|
+ min, //分
|
|
|
+ s, //秒
|
|
|
+ ms //毫秒
|
|
|
+ );
|
|
|
+ mResponser.onUpdateData(GATT_CHARACTERISTIC_NOTIFICATION_CHANGED, "蓝牙数据回调刷新对象", dataEvent);
|
|
|
+ } else {
|
|
|
+ //直接返回字符串
|
|
|
+ DataEvent dataEvent = new DataEvent(
|
|
|
+ "STRING",
|
|
|
+ Conversion.hexStringToString(_hexStr)
|
|
|
+ );
|
|
|
+ mResponser.onUpdateData(GATT_CHARACTERISTIC_NOTIFICATION_STRING, "蓝牙数据回调JSON字符串", dataEvent);
|
|
|
}
|
|
|
- } else if (!"com.example.bluetooth.le.ACTION_DATA_WRITE_FAIL".equals(action) && !"com.example.bluetooth.le.ACTION_DATA_WRITE_SUCCESS".equals(action)) {
|
|
|
- if ("com.example.bluetooth.le.ACTION_DATA_READ_FAIL".equals(action)) {
|
|
|
- } else if ("com.example.bluetooth.le.QUERY_SUCCESS".equals(action)) {
|
|
|
- } else if ("com.example.bluetooth.le.ACTION_NOTIFY_SUCCESS".equals(action)) {
|
|
|
- a = intent.getStringExtra("com.example.bluetooth.le.EXTRA_DATA");
|
|
|
-
|
|
|
- } else if ("com.example.bluetooth.le.ACTION_NOTIFY_FAIL".equals(action)) {
|
|
|
- } else if ("com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED_FAIL".equals(action)) {
|
|
|
+
|
|
|
+
|
|
|
+ } else if (!BluetoothLeService.ACTION_DATA_WRITE_FAIL.equals(action) && !BluetoothLeService.ACTION_DATA_WRITE_SUCCESS.equals(action)) {
|
|
|
+ if (BluetoothLeService.ACTION_DATA_READ_FAIL.equals(action)) {
|
|
|
+ } else if (BluetoothLeService.ACTION_QUERY_SUCCESS.equals(action)) {
|
|
|
+ } else if (BluetoothLeService.ACTION_NOTIFY_SUCCESS.equals(action)) {
|
|
|
+ _hexStr = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
|
|
+
|
|
|
+ } else if (BluetoothLeService.ACTION_NOTIFY_FAIL.equals(action)) {
|
|
|
+ } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED_FAIL.equals(action)) {
|
|
|
Log.e(TAG, "get service fail");
|
|
|
if (retryNo > 0) {
|
|
|
flagDiscoverFail = true;
|
|
|
@@ -170,8 +320,8 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
} else {
|
|
|
mBluetoothLeService.disconnect();
|
|
|
}
|
|
|
- } else if ("android.bluetooth.device.action.BOND_STATE_CHANGED".equals(action)) {
|
|
|
- BluetoothDevice aaa = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
|
|
|
+ } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
|
|
|
+ BluetoothDevice aaa = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
if (aaa.getBondState() == 12) {
|
|
|
Log.e(TAG, "bonded");
|
|
|
} else if (aaa.getBondState() == 11) {
|
|
|
@@ -179,7 +329,7 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
} else {
|
|
|
Log.e(TAG, "unbound");
|
|
|
}
|
|
|
- } else if ("android.bluetooth.device.action.PAIRING_REQUEST".equals(action)) {
|
|
|
+ } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
@@ -194,6 +344,9 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
|
|
|
+ intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE_SUCCESS);
|
|
|
+ intentFilter.addAction(BluetoothLeService.ACTION_RESPONSE_FAIL);
|
|
|
+ intentFilter.addAction(BluetoothLeService.ACTION_RESPONSE_SUCCESS);
|
|
|
//vers add
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_DATA_WRITE_FAIL);
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_DATA_WRITE_SUCCESS);
|
|
|
@@ -215,7 +368,7 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
mBluetoothAdapter.stopLeScan(mLeScanCallback);
|
|
|
- mResponser.onSuccess(new DeviceEvent("none"));
|
|
|
+ mResponser.onSuccess(STOP_SCAN_BLUETOOTH, "停止扫描蓝牙。", null);
|
|
|
}
|
|
|
};
|
|
|
// Device scan callback.
|
|
|
@@ -223,23 +376,24 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
new BluetoothAdapter.LeScanCallback() {
|
|
|
@Override
|
|
|
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
|
- //todo 返回查找到的设备对象
|
|
|
String _name = device.getName();
|
|
|
- if (_name != null && _name.indexOf("BGBox") > -1) {
|
|
|
- mDeviceAddress = device.getAddress();
|
|
|
- mDeviceName = device.getName();
|
|
|
- if (mBluetoothLeService != null) {
|
|
|
- boolean result =mBluetoothLeService.connect(mDeviceAddress);
|
|
|
- Log.e(TAG, "device Connect request result =" + result);
|
|
|
+ if (_name != null && _name.indexOf(searName) > -1) {
|
|
|
+ //获取到已经连接的设备 ,进行连接操作
|
|
|
+ boolean _bSuccess = mBluetoothLeService.connect(mDeviceAddress);
|
|
|
+ if (_bSuccess) {
|
|
|
+ mDeviceAddress = device.getAddress();
|
|
|
+ mDeviceName = device.getName();
|
|
|
+ DeviceEvent outDevice = new DeviceEvent("device", device, rssi);
|
|
|
+ outDevice.setDeviceAddress(mDeviceAddress);
|
|
|
+ outDevice.setDeviceName(mDeviceName);
|
|
|
+ mResponser.onSuccess(CONNECT_BLUETOOTH, "设备成功连接", outDevice);
|
|
|
+ stopConnectLeDevice();
|
|
|
+ } else {
|
|
|
+ mResponser.onFailed(UNABLE_CONNECT_BLUETOOTH, "搜索连接失败!");
|
|
|
}
|
|
|
|
|
|
- DeviceEvent outDevice = new DeviceEvent("device", device, rssi);
|
|
|
- outDevice.setDeviceAddress(mDeviceAddress);
|
|
|
- outDevice.setDeviceName(mDeviceName);
|
|
|
- mResponser.onSuccess(outDevice);
|
|
|
- stopScanLeDevice();
|
|
|
} else {
|
|
|
- mResponser.onFailed(10003, "搜索连接失败!");
|
|
|
+ mResponser.onFailed(UNABLE_CONNECT_BLUETOOTH, "搜索连接失败!");
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
@@ -251,78 +405,142 @@ public class MainBluetoothImpl implements MainBluetooth {
|
|
|
* @param context
|
|
|
*/
|
|
|
@Override
|
|
|
- public void onCreate(Context context, Responser responser) {
|
|
|
+ public void onRegisterEvent(Context context, Responser responser) {
|
|
|
+ Log.w(TAG, "*************************onCreate**********************");
|
|
|
//回调赋值
|
|
|
mResponser = responser;
|
|
|
mHandler = new Handler();
|
|
|
mContext = context;
|
|
|
Intent gattServiceIntent = new Intent(mContext, BluetoothLeService.class);
|
|
|
- context.bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
|
|
|
- context.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
|
|
|
+ mContext.bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
|
|
|
+ mContext.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onDestroy() {
|
|
|
- this.mContext.unregisterReceiver(this.mGattUpdateReceiver);
|
|
|
- this.mBluetoothLeService.disconnect();
|
|
|
- this.mContext.unbindService(this.mServiceConnection);
|
|
|
- this.mBluetoothLeService = null;
|
|
|
+ public void onUnregisterEvent() {
|
|
|
+ Log.w(TAG, "*****************************onDestroy***********************");
|
|
|
+ mBluetoothLeService.disconnect();
|
|
|
+ mContext.unregisterReceiver(mGattUpdateReceiver);
|
|
|
+ mContext.unbindService(mServiceConnection);
|
|
|
}
|
|
|
|
|
|
- //扫描设备,包括返回已连接的设备
|
|
|
+ //扫描设备,并连接
|
|
|
@Override
|
|
|
- public void startScanLeDevice(String searName) {
|
|
|
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
|
|
|
- //todo 返回已连接的设备对象
|
|
|
- if (pairedDevices.size() != 0) {
|
|
|
- Iterator var3 = pairedDevices.iterator();
|
|
|
- while(var3.hasNext()) {
|
|
|
- BluetoothDevice device = (BluetoothDevice)var3.next();
|
|
|
- if (device.getName().indexOf("BGBox") > -1) {
|
|
|
- Log.e(TAG, "**********DeviceEvent responser2=" + device.getAddress() + "=" + device.getName());
|
|
|
- this.mDeviceAddress = device.getAddress();
|
|
|
- this.mDeviceName = device.getName();
|
|
|
- if (this.mBluetoothLeService != null) {
|
|
|
- boolean result = this.mBluetoothLeService.connect(this.mDeviceAddress);
|
|
|
- Log.e(TAG, "pairedDevices Connect request result =" + result);
|
|
|
+ public void startConnectLeDevice(String searNameTemp) {
|
|
|
+ searName = searNameTemp;
|
|
|
+ Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
|
|
|
+ try {//得到连接状态的方法
|
|
|
+ Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
|
|
|
+ //打开权限
|
|
|
+ method.setAccessible(true);
|
|
|
+ //获取蓝牙已匹配的设备
|
|
|
+ Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
|
|
|
+ if (pairedDevices.size() != 0) {
|
|
|
+ Iterator pairedDevice = pairedDevices.iterator();
|
|
|
+ boolean _bConnectSuccess = false;
|
|
|
+ while (pairedDevice.hasNext()) {
|
|
|
+ BluetoothDevice device = (BluetoothDevice) pairedDevice.next();
|
|
|
+ //获取名称为BGBox的设备
|
|
|
+ if (device.getName().indexOf(searName) > -1) {
|
|
|
+ Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
|
|
|
+ method.setAccessible(true);
|
|
|
+ boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
|
|
|
+ if (isConnected) {
|
|
|
+ Log.i(TAG, "connected:" + device.getName() + " = " + device.getAddress());
|
|
|
+ //获取到已经连接的设备 ,进行连接操作
|
|
|
+ boolean _bSuccess = mBluetoothLeService.connect(device.getAddress());
|
|
|
+ if (_bSuccess) {
|
|
|
+ mDeviceAddress = device.getAddress();
|
|
|
+ mDeviceName = device.getName();
|
|
|
+ DeviceEvent devices = new DeviceEvent("pairedDevices", pairedDevices);
|
|
|
+ devices.setDeviceAddress(mDeviceAddress);
|
|
|
+ devices.setDeviceName(mDeviceName);
|
|
|
+ mResponser.onSuccess(CONNECT_BLUETOOTH, "设备成功连接", devices);
|
|
|
+ _bConnectSuccess = _bSuccess;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- DeviceEvent devices = new DeviceEvent("pairedDevices", pairedDevices);
|
|
|
- devices.setDeviceAddress(this.mDeviceAddress);
|
|
|
- devices.setDeviceName(this.mDeviceName);
|
|
|
- this.mResponser.onSuccess(devices);
|
|
|
- return;
|
|
|
}
|
|
|
+ //todo 如果有匹配的设备,但是没有连接的。提示需要跳转手机蓝牙页面操作
|
|
|
+ if (!_bConnectSuccess) {
|
|
|
+ mResponser.onFailed(BONED_NOT_CONNECTED, "匹配列表设备未连接。");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //没有匹配的设备,搜索设备
|
|
|
+ mBluetoothAdapter.startLeScan(mLeScanCallback);
|
|
|
+ mHandler.postDelayed(mGetVersionTimeoutThread, 10000L);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- this.mBluetoothAdapter.startLeScan(this.mLeScanCallback);
|
|
|
- this.mHandler.postDelayed(this.mGetVersionTimeoutThread, 10000L);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void writeValueToFFF(String value, WriteResponser writeResponser) {
|
|
|
+ public void stopConnectLeDevice() {
|
|
|
+ mBluetoothAdapter.stopLeScan(mLeScanCallback);
|
|
|
+ mHandler.removeCallbacks(mGetVersionTimeoutThread);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void disConnectLeDevice() {
|
|
|
+
|
|
|
+ //先发送一个关闭指令到蓝牙
|
|
|
+ String str = Conversion.str2HexStr("4");
|
|
|
+ byte[] result = Conversion.hexStringToBytes(str);
|
|
|
+ UUID service = UUID.fromString(GattAttributes.UUID_FFF_service);
|
|
|
+ UUID characteristic = UUID.fromString(GattAttributes.UUID_FFF_characteristic_write);
|
|
|
+ mBluetoothLeService.setServiceandCharacteristic(service, characteristic, 0);
|
|
|
+ mBluetoothLeService.writeBLECharacteristicValue(result);
|
|
|
+
|
|
|
+ Timer timer = new Timer();
|
|
|
+ timer.schedule(new TimerTask() {
|
|
|
+ public void run() {
|
|
|
+ //断开蓝牙连接
|
|
|
+ mBluetoothLeService.disconnect();
|
|
|
+ }
|
|
|
+ }, 200);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeValueToFFF(String value) {
|
|
|
+
|
|
|
+ if (value.equals("3") || value.equals("4")) {
|
|
|
+ bParsingData = true;
|
|
|
+ } else if (value.equals("V")) {
|
|
|
+ bParsingData = false;
|
|
|
+ }
|
|
|
+
|
|
|
String str = Conversion.str2HexStr(value);
|
|
|
byte[] result = Conversion.hexStringToBytes(str);
|
|
|
- UUID service = UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB");
|
|
|
- UUID characteristic = UUID.fromString("0000FFF2-0000-1000-8000-00805F9B34FB");
|
|
|
- int mode = 0;
|
|
|
- this.mWriteResponser = writeResponser;
|
|
|
- this.mBluetoothLeService.setServiceandCharacteristic(service, characteristic, mode);
|
|
|
- this.mBluetoothLeService.writeBLECharacteristicValue(result, writeResponser);
|
|
|
+ UUID service = UUID.fromString(GattAttributes.UUID_FFF_service);
|
|
|
+ UUID characteristic = UUID.fromString(GattAttributes.UUID_FFF_characteristic_write);
|
|
|
+ mBluetoothLeService.setServiceandCharacteristic(service, characteristic, 0);
|
|
|
+ mBluetoothLeService.writeBLECharacteristicValue(result);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void stopScanLeDevice() {
|
|
|
- mBluetoothAdapter.stopLeScan(mLeScanCallback);
|
|
|
- mHandler.removeCallbacks(mGetVersionTimeoutThread);
|
|
|
+ public void findNotifyCharacteristic(TextView textView) {
|
|
|
+ UUID service = UUID.fromString(GattAttributes.UUID_FFF_service);
|
|
|
+ UUID characteristic = UUID.fromString(GattAttributes.UUID_FFF_characteristic_write);
|
|
|
+ mBluetoothLeService.findNotifyCharacteristic(service, characteristic, textView);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public BluetoothAdapter getBluetoothAdapter() {
|
|
|
return mBluetoothAdapter;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public BluetoothLeService getBluetoothLeService() {return this.mBluetoothLeService; }
|
|
|
+ public BluetoothLeService getBluetoothLeService() {
|
|
|
+ return mBluetoothLeService;
|
|
|
+ }
|
|
|
}
|