An #AndroidPort implementation for connecting to a HM-10 (Bluetooth LE).
| 40 | * LE). |
| 41 | */ |
| 42 | public class HM10Port |
| 43 | extends BluetoothGattCallback |
| 44 | implements AndroidPort { |
| 45 | private static final String TAG = "XCSoar"; |
| 46 | |
| 47 | private static final int MAX_WRITE_CHUNK_SIZE = 20; |
| 48 | |
| 49 | /* Maximum number of milliseconds to wait for disconnected state after |
| 50 | calling BluetoothGatt.disconnect() in close() */ |
| 51 | private static final int DISCONNECT_TIMEOUT = 500; |
| 52 | |
| 53 | private PortListener portListener; |
| 54 | private volatile InputListener listener; |
| 55 | private final SafeDestruct safeDestruct = new SafeDestruct(); |
| 56 | |
| 57 | private final BluetoothGatt gatt; |
| 58 | private BluetoothGattCharacteristic dataCharacteristic; |
| 59 | private BluetoothGattCharacteristic deviceNameCharacteristic; |
| 60 | private volatile boolean shutdown = false; |
| 61 | |
| 62 | private final HM10WriteBuffer writeBuffer = new HM10WriteBuffer(); |
| 63 | |
| 64 | private volatile int portState = STATE_LIMBO; |
| 65 | |
| 66 | private final Object gattStateSync = new Object(); |
| 67 | private int gattState = BluetoothGatt.STATE_DISCONNECTED; |
| 68 | |
| 69 | public HM10Port(Context context, BluetoothDevice device) |
| 70 | throws IOException |
| 71 | { |
| 72 | if (Build.VERSION.SDK_INT >= 23) |
| 73 | gatt = device.connectGatt(context, true, this, BluetoothDevice.TRANSPORT_LE); |
| 74 | else |
| 75 | gatt = device.connectGatt(context, true, this); |
| 76 | |
| 77 | if (gatt == null) |
| 78 | throw new IOException("Bluetooth GATT connect failed"); |
| 79 | } |
| 80 | |
| 81 | private void findCharacteristics() throws Error { |
| 82 | dataCharacteristic = null; |
| 83 | deviceNameCharacteristic = null; |
| 84 | |
| 85 | BluetoothGattService service = gatt.getService(BluetoothUuids.HM10_SERVICE); |
| 86 | if (service != null) { |
| 87 | dataCharacteristic = service.getCharacteristic(BluetoothUuids.HM10_RX_TX_CHARACTERISTIC); |
| 88 | } |
| 89 | |
| 90 | service = gatt.getService(BluetoothUuids.GENERIC_ACCESS_SERVICE); |
| 91 | if (service != null) { |
| 92 | deviceNameCharacteristic = service.getCharacteristic(BluetoothUuids.DEVICE_NAME_CHARACTERISTIC); |
| 93 | } |
| 94 | |
| 95 | if (dataCharacteristic == null) |
| 96 | throw new Error("HM10 data characteristic not found"); |
| 97 | |
| 98 | if (deviceNameCharacteristic == null) |
| 99 | throw new Error("GATT device name characteristic not found"); |
nothing calls this directly
no outgoing calls
no test coverage detected