android开发环境设置和BLE驱动开发说明.doc

上传人:scccc 文档编号:12459232 上传时间:2021-12-04 格式:DOC 页数:12 大小:58KB
返回 下载 相关 举报
android开发环境设置和BLE驱动开发说明.doc_第1页
第1页 / 共12页
android开发环境设置和BLE驱动开发说明.doc_第2页
第2页 / 共12页
android开发环境设置和BLE驱动开发说明.doc_第3页
第3页 / 共12页
android开发环境设置和BLE驱动开发说明.doc_第4页
第4页 / 共12页
android开发环境设置和BLE驱动开发说明.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《android开发环境设置和BLE驱动开发说明.doc》由会员分享,可在线阅读,更多相关《android开发环境设置和BLE驱动开发说明.doc(12页珍藏版)》请在三一文库上搜索。

1、Android开发环境设置和Bloothlow energy(BLE)驱动开发说明卢进铭Android开发环境设置 一、要安装android 4.3(API18),否则会出现 各种错误,如导入包会出问题。建议:最 好先安装android 4.3(API18)的Packages。然 后再导入工程,这时开发环境会根据SDK的 版本选择相应的tookBLE驱动开发说明 一、Android Manifest.xml相关设置 1、软件版本最小至少要为28,设置如下 <uses-sdk android:minSdkVersion二"78 2、设置uses-feature,说明此程序需要的相

2、应硬件,设置如下 <uses-featureandrod:name=/android.hardware.bluetooth_le " android:required =Htrue11 /> 3、设iuses-permission,说明此程序需要的相关的权限 <uses-permission android:name= "android.permission.BLUETOOTH"/> <uses-permissionandroidmame 二"android, permission. BLUETOOTH_ADMIN"

3、; />BLE代码说明、判断系统是否支持BLE,代码如下:if(!getPackageManager().hasSystemFeature(Pac kageNanager.FEATURE_BLUETOOTH_LE)Joast.makeTextfthis,Rstring.ble_not_supporte*Toast丄 ENGTH_SHORT).show(); fin ish();二、得到系统蓝牙的适配器,代码如下:final BluetoothManager bluetoothManager =(BluetoothManager)getSysterrSer/'ceCortext.B

4、LUETOOTH_SERVICE); mBIuetoothAdapter = bluetoothMa nager.getAdapter(); / Checks if Bluetooth is supported on the device.if (mBIuetoothAdapter = null) Toast.makeText(this, R.string.errorbluetoothnotsupported, 帀 ast 丄 ENGTH_SHORT).show(); 一" finishf); return;三、判断蓝牙是否打开,代码如下: if (!mBluetoothAdapte

5、r.isEnabled() if (!mBluetoothAdapter.isEnabled() Intent en ableBtl ntent = newlntent(BluetoothAdapter.ZlCT/O/y_/?EQUES T_EN ABLE);_ startActivityForResult(e nableBtl ntent, REQUES T_ENABLE_BT);响应打开蓝牙的操作,代码如下: protected void onActivityResult(int requestcode, int resultCode, Intent data) / User chose

6、not to enable Bluetooth. if (requestcode = REQUEST_ENABLE_BT && resultcode = Activity.RESUL T_C入 NCELE” fin ish(); return; super.onActivityResult(requestCode, resultCode, data); 四、调用蓝牙适配器的扫描设备函数,寻找设备。 mBluetoothAdapter.startLeSca n( mLeSca nCallback); / Device sean callback private Bluetooth

7、Adapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() (©Override public void onLeScan(final BluetoothDevice device, int rssi, bytescanRecord) runOnUiThread(new Runnable() (©Override public void run() mLeDeviceListAdapter.addDevice(device); mLeDeviceListAdapter. no

8、tifyDataSetCha nged(); ); ;寻找设备一段时间后,记得调用 mBluetoothAdapter.stopLeSca n( mLeSca nCallback);四、通过扫描得到的地址寻找设备,然后 调用设备的connectGatt与设备进行连接, 代码如下: Final BluetoothDevice device=mBluetooth.getRemoteDevice(address ); Device.c onn ectGattfthis,false,mGattCallback);mGattCallback 的类型为 BluetoothGattCallback,该类型处

9、理GATT的 各种事件,如设备连接完毕或断开五、成功连接设备后,调用类BluetoothGatt 中getServices,得到BluetoothGattService的 集合,代码如下: mBluetoothGatt.getServices();六、可以调用类BluetoothGattService中的方 法来得到设备相关的服务信息,女口: getUuid()可以用来得到服务的UUID, getCharacteristics()来得到相关的特性,也可 调用类 BluetoothGattCharacteristic 中的方法 getUuid()来获得相关特性的UUID。代码如 下: Uuid=gattService.getUuid(); List<BluetoothGattCharacteristic> gattCharacteristic=gattService.getCharacteristi c();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1