我试过this, 我能够扫描所有设备,但无法使用Android核心类计算主要和次要设备。
我试过这个
public class TagBluetooth {
private Context context;
private List uuidList;
private iTagBle iTagble;
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private BluetoothLeScanner bluetoothLeScanner;
private final int REQUEST_ENABLE_BT = 101;
private boolean mScanning;
private Handler handler;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private BluetoothAdapter.LeScanCallback leScanCallback;
private List list;
public TagBluetooth(iTagBle activity){
this.context= (Context) activity;
iTagble=activity;
}
public TagBluetooth(iTagBle activity,List list){
this.context= (Context) activity;
iTagble=activity;
uuidList=list;
}
/**
* initialize ble component
*/
public void initialize(){
if (isBLEAvailable()) {
bluetoothManager = (BluetoothManager) context.getSystemService(context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
handler = new Handler();
leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
iTagble.onScanComplete(device,rssi,scanRecord);
}
};
}
checkBluetoothStatus();
}
/**
* Ensures Bluetooth is available on the device and it is enabled. If not,
* displays a dialog requesting user permission to enable Bluetooth.
*
* @return true for device bluetooth available and false for bluetooth not available
*/
private void checkBluetoothStatus() {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivity(enableBtIntent);
}
}
/**
* ebale and disable ble devices scanning
*
* @param enable
*/
public void scanLeDevice(final boolean enable) {
if(!isBLEAvailable())
return;
if (enable) {
// Stops scanning after a pre-defined scan period.
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
bluetoothAdapter.stopLeScan(leScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
bluetoothAdapter.startLeScan(leScanCallback);
} else {
mScanning = false;
bluetoothAdapter.stopLeScan(leScanCallback);
}
}
/**
* Use this check to determine whether BLE is supported on the device. Then
* you can selectively disable BLE-related features.
*
* @return true for BLE support and false for BLE unsupported
*/
private boolean isBLEAvailable() {
if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
iTagble.showMessage("BLE not supported");
return false;
}
return true;
}
}
要求:
如果有人帮助我,我会感激不尽。 提前谢谢。