Back to project page BleSensorTag.
The source code is released under:
MIT License
If you think the Android project BleSensorTag listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package sample.ble.sensortag.ble; //from ww w . j av a2s .c om import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothManager; import android.content.Context; import android.content.pm.PackageManager; public class BleUtils { public static final int STATUS_BLE_ENABLED = 0; public static final int STATUS_BLUETOOTH_NOT_AVAILABLE = 1; public static final int STATUS_BLE_NOT_AVAILABLE = 2; public static final int STATUS_BLUETOOTH_DISABLED = 3; public static BluetoothAdapter getBluetoothAdapter(Context context) { // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // BluetoothAdapter through BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) return null; return bluetoothManager.getAdapter(); } public static int getBleStatus(Context context) { // Use this check to determine whether BLE is supported on the device. Then you can // selectively disable BLE-related features. if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { return STATUS_BLE_NOT_AVAILABLE; } final BluetoothAdapter adapter = getBluetoothAdapter(context); // Checks if Bluetooth is supported on the device. if (adapter == null) { return STATUS_BLUETOOTH_NOT_AVAILABLE; } if (adapter.isEnabled()) return STATUS_BLUETOOTH_DISABLED; return STATUS_BLE_ENABLED; } public static BleGattExecutor createExecutor(final BleExecutorListener listener) { return new BleGattExecutor() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); listener.onConnectionStateChange(gatt, status, newState); } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); listener.onServicesDiscovered(gatt, status); } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); listener.onCharacteristicRead(gatt, characteristic, status); } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); listener.onCharacteristicChanged(gatt, characteristic); } }; } }