public class ScanDevices{
private String SERVICESUUID = "0000ffe0-0000-1000-8000-00805f9b34fb"; //服务的UUID
private String WRITEUUID = "0000ffe3-0000-1000-8000-00805f9b34fb"; //写入特征的UUID
private BluetoothDevice mDevice;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt bluetoothGatt;
private BluetoothGattService bluetoothGattService;
private BluetoothGattCharacteristic writeCharacteristic;
private final List<BluetoothDevice> BlueToothDevice_Info = new ArrayList<>();//蓝牙设备信息
private Handler mHandler;
private static Context mContext;
public void setContext(Context context){
mContext = context;
}
/*
扫描设备
*/
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
stopScan();
}
}, 5000); //5秒后停止搜索
startScan();
} else {
stopScan();
}
}
/*
开始扫描设备
*/
private void startScan(){
mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索
}
/*
停止扫描设备
*/
private void stopScan(){
mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索
}
/**
* 扫描设备回调
*/
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback(){
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if(device.getName() != null){
((Activity) mContext).runOnUiThread(new Runnable(){
@Override
public void run() {
if (!BlueToothDevice_Info.contains(device)){
BlueToothDevice_Info.add(device);
}
}
});
mDevice = mBluetoothAdapter.getRemoteDevice(device.toString());
//第二个参数,是否重连
bluetoothGatt = mDevice.connectGatt(mContext,false,bluetoothGattCallback);
}
}
};
/*
蓝牙连接成功回调
*/
private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
//连接状态改变时回调
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED){
Log.d("onConnectionStateChange","连接成功");
//发现服务
gatt.discoverServices();
}else if (newState == BluetoothProfile.STATE_DISCONNECTED){
Log.d("onConnectionStateChange","断开连接");
if (mDevice != null){
//关闭当前新的连接
gatt.close();
}
}
}
//写入成功回调函数
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS){
Log.d("onCharacteristicWrite","写入成功");
}
}
//UUID搜索成功回调
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d("onCharacteristicWrite","写入成功");
if (status == BluetoothGatt.GATT_SUCCESS){
List<BluetoothGattService> supportedGattServices = bluetoothGatt.getServices();
for (int i = 0; i < supportedGattServices.size(); i++) {
Log.i("success", "1:BluetoothGattService UUID=:" + supportedGattServices.get(i).getUuid());
List<BluetoothGattCharacteristic> listGattCharacteristic = supportedGattServices.get(i).getCharacteristics();
for (int j = 0; j < listGattCharacteristic.size(); j++) {
Log.i("success", "2: BluetoothGattCharacteristic UUID=:" + listGattCharacteristic.get(j).getUuid());
}
}
}else{
Log.e("fail","onservicesdiscovered收到:" + status);
}
//设置serviceUUID
bluetoothGattService = bluetoothGatt.getService(UUID.fromString(SERVICESUUID));
//设置写入特征UUID
writeCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(WRITEUUID));
//开启监听
Log.d("uuidconnectsuccess","uuid连接成功");
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
byte[] value = characteristic.getValue();
}
};
//如何将我扫描得到的BlueToothDevice_Info能够通过这个函数传递出去
public List<SysScanResult> getSysResultList(){
}
}
//SysScanResult是我写的一个List的实体类,代码如下
public class SysScanResult {
public String ssid;
public int rssi;
public SysScanResult(String var1, int var2) {
this.ssid = var1;
this.rssi = var2;
}
public boolean equals(Object var1) {
if (this == var1) {
return true;
} else if (var1 != null && this.getClass() == var1.getClass()) {
SysScanResult var2 = (SysScanResult)var1;
if (this.ssid != null) {
return this.ssid.equals(var2.ssid);
} else {
return var2.ssid == null;
}
} else {
return false;
}
}
public int hashCode() {
return this.ssid != null ? this.ssid.hashCode() : 0;
}
public String toString() {
return "{ssid='" + this.ssid + '\'' + ", rssi=" + this.rssi + '}';
}
}