几天前,我在Google Play商店下载了Bluetooth Smart Scanner软件.在我的三星Galaxy S3手机上安装后,我可以成功扫描我的蓝牙LE设备.
尽管我已经尝试过扫描我的蓝牙LE设备的所有方法,但我的计算机上没有显示任何内容.所以我反编译了这个软件,startLeDiscovery()包中有一个方法android.bluetooth.但令我困惑的是,这个方法在我android.jar的android sdk 15中不存在.
最后,我用Bluetooth Smart Scanner软件替换了它们的BlutoothAdapter.class文件和BluetoothDevice.class文件android.jar,这样我就可以startLeDiscovery()在Eclipse中成功调用该方法.源代码如下图所示.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);
intent.addAction(BluetoothDevice.EXTRA_UUID);
intent.addAction(BluetoothDevice.ACTION_GATT_PRIMARY_UUID);
registerReceiver(searchDevices, intent);
//bluetooth.discovery();
bluetooth.startLeDiscovery();
}
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = null;
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String msg = device.getName()+"\n"+device.getAddress();
Log.i("hella",msg);
bluetooth.cancelDiscovery();
connectDevice(device); …Run Code Online (Sandbox Code Playgroud)