mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-23 16:53:04 +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.BluetoothGattCharacteristic;
|
||||||
import android.bluetooth.BluetoothGattDescriptor;
|
import android.bluetooth.BluetoothGattDescriptor;
|
||||||
import android.bluetooth.BluetoothProfile;
|
import android.bluetooth.BluetoothProfile;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -46,12 +49,12 @@ public abstract class BluetoothCommunication {
|
|||||||
protected Context context;
|
protected Context context;
|
||||||
|
|
||||||
private Handler callbackBtHandler;
|
private Handler callbackBtHandler;
|
||||||
private BluetoothGatt bluetoothGatt;
|
private static BluetoothGatt bluetoothGatt;
|
||||||
protected BluetoothGattCallback gattCallback;
|
protected BluetoothGattCallback gattCallback;
|
||||||
private BluetoothAdapter.LeScanCallback scanCallback;
|
|
||||||
protected BluetoothAdapter btAdapter;
|
protected BluetoothAdapter btAdapter;
|
||||||
private Handler searchHandler;
|
private Handler searchHandler;
|
||||||
private String btDeviceName;
|
private String btDeviceName;
|
||||||
|
public boolean isReceiverRegistered;
|
||||||
|
|
||||||
private int cmdStepNr;
|
private int cmdStepNr;
|
||||||
private int initStepNr;
|
private int initStepNr;
|
||||||
@@ -67,8 +70,9 @@ public abstract class BluetoothCommunication {
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
searchHandler = new Handler();
|
searchHandler = new Handler();
|
||||||
scanCallback = null;
|
|
||||||
gattCallback = new GattCallback();
|
gattCallback = new GattCallback();
|
||||||
|
isReceiverRegistered = false;
|
||||||
|
bluetoothGatt = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -411,57 +415,60 @@ public abstract class BluetoothCommunication {
|
|||||||
public void startSearching(String deviceName) {
|
public void startSearching(String deviceName) {
|
||||||
btDeviceName = deviceName;
|
btDeviceName = deviceName;
|
||||||
|
|
||||||
if (scanCallback == null)
|
IntentFilter filter = new IntentFilter();
|
||||||
{
|
|
||||||
scanCallback = new BluetoothAdapter.LeScanCallback()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
||||||
try {
|
|
||||||
if (device.getName() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (device.getName().toLowerCase().equals(btDeviceName.toLowerCase())) {
|
filter.addAction(BluetoothDevice.ACTION_FOUND);
|
||||||
Log.d("BluetoothCommunication", btDeviceName + " found trying to connect...");
|
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||||
|
|
||||||
searchHandler.removeCallbacksAndMessages(null);
|
context.registerReceiver(mReceiver, filter);
|
||||||
btAdapter.stopLeScan(scanCallback);
|
isReceiverRegistered = true;
|
||||||
bluetoothGatt = device.connectGatt(context, false, gattCallback);
|
btAdapter.startDiscovery();
|
||||||
}
|
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
* Stop searching for a Bluetooth device
|
||||||
*/
|
*/
|
||||||
public void stopSearching() {
|
public void stopSearching() {
|
||||||
if (bluetoothGatt != null)
|
if (bluetoothGatt != null)
|
||||||
{
|
{
|
||||||
|
bluetoothGatt.disconnect();
|
||||||
bluetoothGatt.close();
|
bluetoothGatt.close();
|
||||||
bluetoothGatt = null;
|
bluetoothGatt = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isReceiverRegistered == true) {
|
||||||
|
context.unregisterReceiver(mReceiver);
|
||||||
|
isReceiverRegistered = false;
|
||||||
|
}
|
||||||
|
|
||||||
searchHandler.removeCallbacksAndMessages(null);
|
searchHandler.removeCallbacksAndMessages(null);
|
||||||
btAdapter.stopLeScan(scanCallback);
|
btAdapter.cancelDiscovery();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user