Android BLE扫描、连接、切断连续100次代码
时间: 2023-07-31 13:10:35 浏览: 44
以下是一个简单的 Android BLE 扫描、连接、切断的代码,在进行 100 次操作时,会打印出每一次的操作结果:
```
public class BleScanConnectDisconnect {
private static final String TAG = "BleScanConnectDisconnect";
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private ScanCallback mScanCallback;
private BluetoothGatt mBluetoothGatt;
private Handler mHandler;
public BleScanConnectDisconnect(Context context) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
mHandler = new Handler();
}
public void startScan() {
Log.d(TAG, "startScan");
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.d(TAG, "onScanResult: " + result.getDevice().getName() + " " + result.getDevice().getAddress());
}
@Override
public void onScanFailed(int errorCode) {
Log.e(TAG, "onScanFailed: " + errorCode);
}
};
mBluetoothLeScanner.startScan(mScanCallback);
}
public void stopScan() {
Log.d(TAG, "stopScan");
mBluetoothLeScanner.stopScan(mScanCallback);
}
public void connect(String address) {
Log.d(TAG, "connect: " + address);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mBluetoothGatt = device.connectGatt(context, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.d(TAG, "onConnectionStateChange: " + newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "onServicesDiscovered: " + status);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "onCharacteristicRead: " + characteristic.getUuid());
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "onCharacteristicWrite: " + characteristic.getUuid());
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid());
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.d(TAG, "onDescriptorRead: " + descriptor.getUuid());
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.d(TAG, "onDescriptorWrite: " + descriptor.getUuid());
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
Log.d(TAG, "onReliableWriteCompleted");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
Log.d(TAG, "onReadRemoteRssi: " + rssi);
}
});
}
public void disconnect() {
Log.d(TAG, "disconnect");
if (mBluetoothGatt != null) {
mBluetoothGatt.disconnect();
}
}
public void close() {
Log.d(TAG, "close");
if (mBluetoothGatt != null) {
mBluetoothGatt.close();
}
}
public void test() {
for (int i = 0; i < 100; i++) {
Log.d(TAG, "============================= " + (i + 1) + " =============================");
startScan();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
stopScan();
}
}, 5000);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
connect("00:00:00:00:00:00");
}
}, 6000);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
disconnect();
}
}, 8000);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
close();
}
}, 10000);
}
}
}
```
在 `test()` 方法中,我们进行了 100 次操作,每一次操作都包括开始扫描、停止扫描、连接、切断和关闭。每一次操作之间会有一定的时间间隔,可以根据实际情况进行调整。在操作完成后,会打印出每一次操作的结果。