| | |
| | | import android.os.Handler; |
| | | import android.os.Looper; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import com.clj.fastble.conn.BleConnector; |
| | | import com.clj.fastble.conn.BleGattCallback; |
| | |
| | | private Context context; |
| | | private BluetoothAdapter bluetoothAdapter; |
| | | private BluetoothGatt bluetoothGatt; |
| | | private int mtu = 20; |
| | | private Handler handler = new Handler(Looper.getMainLooper()); |
| | | private HashMap<String, BluetoothGattCallback> callbackHashMap = new HashMap<>(); |
| | | private PeriodScanCallback periodScanCallback; |
| | |
| | | bluetoothAdapter = bluetoothManager.getAdapter(); |
| | | } |
| | | |
| | | public void setMtu(int mtu) { |
| | | this.mtu = mtu; |
| | | } |
| | | |
| | | |
| | | public BleConnector newBleConnector() { |
| | | return new BleConnector(this); |
| | | } |
| | | |
| | | |
| | | public boolean isInScanning() { |
| | | return connectionState == STATE_SCANNING; |
| | |
| | | + "\nmac: " + scanResult.getDevice().getAddress() |
| | | + "\nautoConnect: " + autoConnect); |
| | | addConnectGattCallback(callback); |
| | | return scanResult.getDevice().connectGatt(context, autoConnect, coreGattCallback); |
| | | bluetoothGatt = scanResult.getDevice().connectGatt(context, autoConnect, coreGattCallback); |
| | | return bluetoothGatt; |
| | | } |
| | | |
| | | public void scanNameAndConnect(String name, long time_out, final boolean autoConnect, final BleGattCallback callback) { |
| | |
| | | return connectionState; |
| | | } |
| | | |
| | | |
| | | public static String dumpHex(byte[] src) { |
| | | String num = "0123456789ABCDEF"; |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append("[ "); |
| | | for (byte aSrc : src) { |
| | | int high = aSrc >> 4 & 0x0f; |
| | | int low = aSrc & 0x0f; |
| | | sb.append(num.charAt(high)).append(num.charAt(low)).append(" "); |
| | | } |
| | | sb.append(" ]"); |
| | | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private BleGattCallback coreGattCallback = new BleGattCallback() { |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public void onConnecting(BluetoothGatt gatt, int status) { |
| | | BleLog.i("BleGattCallback:onConnectSuccess "); |
| | | |
| | | BleLog.i("BleGattCallback:onConnecting "); |
| | | bluetoothGatt = gatt; |
| | | |
| | | Iterator iterator = callbackHashMap.entrySet().iterator(); |
| | | while (iterator.hasNext()) { |
| | | Map.Entry entry = (Map.Entry) iterator.next(); |
| | |
| | | @Override |
| | | public void onConnectSuccess(BluetoothGatt gatt, int status) { |
| | | BleLog.i("BleGattCallback:onConnectSuccess "); |
| | | |
| | | bluetoothGatt = gatt; |
| | | Iterator iterator = callbackHashMap.entrySet().iterator(); |
| | | while (iterator.hasNext()) { |
| | |
| | | @Override |
| | | public void onServicesDiscovered(BluetoothGatt gatt, int status) { |
| | | BleLog.i("BleGattCallback:onServicesDiscovered "); |
| | | |
| | | bluetoothGatt.requestMtu(mtu); |
| | | connectionState = STATE_SERVICES_DISCOVERED; |
| | | Iterator iterator = callbackHashMap.entrySet().iterator(); |
| | | while (iterator.hasNext()) { |
| | |
| | | @Override |
| | | public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { |
| | | BleLog.i("BleGattCallback:onCharacteristicChanged "); |
| | | |
| | | Iterator iterator = callbackHashMap.entrySet().iterator(); |
| | | while (iterator.hasNext()) { |
| | | Map.Entry entry = (Map.Entry) iterator.next(); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { |
| | | super.onMtuChanged(gatt, mtu, status); |
| | | Log.d("BLE", "onMtuChanged mtu=" + mtu + ",status=" + status); |
| | | |
| | | System.out.println("onMtuChanged-------------------->size:" + mtu); |
| | | if (status == 0) { |
| | | System.out.println("onMtuChanged-------------------->设置成功"); |
| | | } |
| | | } |
| | | }; |
| | | } |