mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-23 08:43:15 +02:00
experimental Bluetooth device connection with new API.
This commit is contained in:
@@ -23,7 +23,10 @@ import android.bluetooth.BluetoothGattCallback;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattDescriptor;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -46,12 +49,12 @@ public abstract class BluetoothCommunication {
|
||||
protected Context context;
|
||||
|
||||
private Handler callbackBtHandler;
|
||||
private BluetoothGatt bluetoothGatt;
|
||||
private static BluetoothGatt bluetoothGatt;
|
||||
protected BluetoothGattCallback gattCallback;
|
||||
private BluetoothAdapter.LeScanCallback scanCallback;
|
||||
protected BluetoothAdapter btAdapter;
|
||||
private Handler searchHandler;
|
||||
private String btDeviceName;
|
||||
public boolean isReceiverRegistered;
|
||||
|
||||
private int cmdStepNr;
|
||||
private int initStepNr;
|
||||
@@ -67,8 +70,9 @@ public abstract class BluetoothCommunication {
|
||||
this.context = context;
|
||||
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
searchHandler = new Handler();
|
||||
scanCallback = null;
|
||||
gattCallback = new GattCallback();
|
||||
isReceiverRegistered = false;
|
||||
bluetoothGatt = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -411,57 +415,60 @@ public abstract class BluetoothCommunication {
|
||||
public void startSearching(String deviceName) {
|
||||
btDeviceName = deviceName;
|
||||
|
||||
if (scanCallback == null)
|
||||
{
|
||||
scanCallback = new BluetoothAdapter.LeScanCallback()
|
||||
{
|
||||
@Override
|
||||
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
||||
try {
|
||||
if (device.getName() == null) {
|
||||
return;
|
||||
}
|
||||
IntentFilter filter = new IntentFilter();
|
||||
|
||||
if (device.getName().toLowerCase().equals(btDeviceName.toLowerCase())) {
|
||||
Log.d("BluetoothCommunication", btDeviceName + " found trying to connect...");
|
||||
filter.addAction(BluetoothDevice.ACTION_FOUND);
|
||||
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||
|
||||
searchHandler.removeCallbacksAndMessages(null);
|
||||
btAdapter.stopLeScan(scanCallback);
|
||||
bluetoothGatt = device.connectGatt(context, false, gattCallback);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
searchHandler.postDelayed(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
btAdapter.stopLeScan(scanCallback);
|
||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||
}
|
||||
}, 10000);
|
||||
|
||||
btAdapter.startLeScan(scanCallback);
|
||||
context.registerReceiver(mReceiver, filter);
|
||||
isReceiverRegistered = true;
|
||||
btAdapter.startDiscovery();
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
|
||||
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
|
||||
//discovery finishes, dismis progress dialog
|
||||
if (bluetoothGatt == null) {
|
||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||
}
|
||||
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
||||
//bluetooth device found
|
||||
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
|
||||
if (device.getName() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (device.getName().toLowerCase().equals(btDeviceName.toLowerCase())) {
|
||||
Log.d("BluetoothCommunication", btDeviceName + " found trying to connect...");
|
||||
|
||||
bluetoothGatt = device.connectGatt(context, true, gattCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop searching for a Bluetooth device
|
||||
*/
|
||||
public void stopSearching() {
|
||||
if (bluetoothGatt != null)
|
||||
{
|
||||
bluetoothGatt.disconnect();
|
||||
bluetoothGatt.close();
|
||||
bluetoothGatt = null;
|
||||
}
|
||||
|
||||
if (isReceiverRegistered == true) {
|
||||
context.unregisterReceiver(mReceiver);
|
||||
isReceiverRegistered = false;
|
||||
}
|
||||
|
||||
searchHandler.removeCallbacksAndMessages(null);
|
||||
btAdapter.stopLeScan(scanCallback);
|
||||
btAdapter.cancelDiscovery();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user