介绍
这篇文章将为大家详细讲解有关android如何实现蓝牙应用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
android实现蓝牙应用的具体代码,具体内容如下
private BluetoothGatt bluetoothGatt; private BluetoothGattService  gattService; private BluetoothGattCharacteristic  gattCharacteristic; private BluetoothManager  bluetoothManager; private BluetoothAdapter  bluetoothAdapter; private List< BluetoothDevice>, devices =, new ArrayList<在(); private UUID  serviceUUID;,,//不同设备,不同uuid private UUID  characteristicUUID;,,,//不同设备,不同uuid private final  UUID service4UUID=, UUID.fromString (“0000 fff0 - 0000 - 1000 - 8000 - 00805 - f9b34fb");,, private final  UUID charAUUID =, UUID.fromString (“0000 fffa - 0000 - 1000 - 8000 - 00805 - f9b34fb"); private LightReceiver  lightReceiver; private ScanReceiver  scanReceiver; private ListView 列表视图; private TextView  tvrefresh; private String  lightAction; @Override protected void  onCreate (Bundle savedInstanceState), { super.onCreate才能(savedInstanceState); ,,setContentView (R.layout.activity_main); Log.i才能(“tag",,“MainActivity , onCreate ()“); ,//listView 才能=,(视图),findViewById (R.id.lv_bluetooth); tvrefresh 才能=,(TextView), findViewById (R.id.tv_refresh_bluetooth); tvrefresh.setOnClickListener才能(这个); openBluetooth才能(); registeLigthReceiver才能(); registeScanReceiver才能(); } @Override protected void  onStart (), { super.onStart才能(); Log.i才能(“tag",,“MainActivity , onStart ()“); bluetoothScan才能(); }//返回 @Override public boolean  onKeyUp (int 键码,KeyEvent 事件),{ Log.i才能(“tag",,“MainActivity , onKeyUp ()“); this.finish才能(); return 才能super.onKeyUp(键码,事件); }//重新扫描蓝牙 @Override public void  onClick (View 视图),{ switch 才能;(view.getId ()), { ,,,case R.id.tv_refresh_bluetooth: ,,,,,//蓝牙扫描 ,,,,,bluetoothScan (); ,,,,,休息; ,,,默认值: ,,,,,休息; ,,} }//打开本地蓝牙 private void  openBluetooth (), { Log.i才能(“tag",,“openLocalBluetooth ()“);//检才能查手机是否支持蓝牙4.0 if 才能;(! getPackageManager () .hasSystemFeature (PackageManager.FEATURE_BLUETOOTH_LE)), { ,,,Toast.makeText(,,“手机不支持蓝牙4.0“,,Toast.LENGTH_SHORT),告诉(); ,,,完成(); ,,}//调才能用系统服务的方式,请求开启蓝牙 bluetoothManager 才能=,(BluetoothManager), getSystemService (Context.BLUETOOTH_SERVICE); 时间=bluetoothAdapter 才能;bluetoothManager.getAdapter ();//开才能启蓝牙 if 才能;(! bluetoothAdapter.isEnabled ()), { ,,,bluetoothAdapter.enable (); ,,} }//开始蓝牙扫描,,扫描到一个添加一个 private void  bluetoothScan (), { Log.i才能(“tag",,“bluetoothScan ()“); if 才能;(bluetoothAdapter ==, null), { ,,,openBluetooth (); ,,} if 才能;(! bluetoothAdapter.isDiscovering ()), { ,,,bluetoothAdapter.startDiscovery();,,//回调 ,,},{else ,,,Toast.makeText(这个,,“扫描中…“,,Toast.LENGTH_SHORT),告诉(); ,,} }//更新蓝牙列表中的数据 private void  updateUi (), { Log.i才能(“tag",,“updateUi ()“); if 才能;(devices !=, null ,,, devices.size(),祝辞,0),{ ,,,BlueAdapter adapter =, new BlueAdapter(,,设备); ,,,listView.setAdapter(适配器); ,,,adapter.notifyDataSetChanged (); ,,},{else ,,,Toast.makeText(,,“附近没有蓝牙“,,Toast.LENGTH_SHORT),告诉(); ,,} }//取得gatt 对应的服务 private BluetoothGattService  getGattService (BluetoothGatt ,关贸总协定,UUID serviceUUID), { List<才能;BluetoothGattService>, gattServices =, gatt.getServices (); for 才能;(BluetoothGattService gattService : gattServices), { ,,,if (gattService.getUuid () .equals (serviceUUID)), { ,,,,,return gattService; ,,,} ,,} return 才能;零; } null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullandroid如何实现蓝牙应用