最近小弟在忙一个蓝牙4.0的case,主要是就是做一个能够跟手机(支持蓝牙4.0)连接的小附件,因为之前玩过cc2540 miniDK 一段时间,所以根据里面的例程基本完成了小附件与dongle 的连接,但如果要跟手机连接的话,很明显需要在手机终端做一个APP,使得手机能跟小附件连接,于是我在moto官网下载了一个demo,文件名叫Motorola_BLE_API_sample(这里给大家传上来啦),相信有很多人都知道哈,等我把domo装手机之后,运行发现会给出:"Bluetooth Low Energy is not supported on this phone !"的提示,纠结了很久,决定厚着脸皮看了一下他的Java程序发现有以下一段代码:
/* Check if Bluetooth Low Energy is supported on phone */
try
{
Class<?> object = Class.forName(className); //className = "android.server.BluetoothGattService";
ifPhoneSupportsLE = true;
} catch (Exception e) {
ifPhoneSupportsLE = false;
} //End logic to check Low Energy support
if (!ifPhoneSupportsLE) {
String message = "Bluetooth Low Energy is not supported on this phone !";
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
finish();
return;
} else {
data[0] = 0x00;
data[1] = 0x00;
// Set up the window layout
setContentView(R.layout.main);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
看了这段程序后,我的理解是它对所用的手机进行了两次检验,
1.执行Class<?> object = Class.forName(className); //className = "android.server.BluetoothGattService";语句看手机安卓系统中是否有android.server.BluetoothGattService这个类,如果没有则输出:"Bluetooth Low Energy is not supported on this phone !"
2.如果上面的检验通过则进行第二次检验,执行mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();然后再查看mBluetoothAdapter 是否为空,为空的话则输出
"Bluetooth is not available"
不知道我的这两点理解对不对,如果不对还请各位指正,谢谢!如果是对的话怎样才能够通过两次校验呢?