当前位置:   article > 正文

Android中蓝牙使用步骤小结_const bluetoothadapter = plus.android.importclass(

const bluetoothadapter = plus.android.importclass('android.bluetooth.bluetoo
  下面小结下android中使用蓝牙的几个步骤

1 导入相关的包:
    import android.bluetooth.*;

2 设置好权限
    <uses-permission android:name="android.permission.BLUETOOTH" />

   如果要更多的高级用户权限设置,要这样
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

3 android中有个BluetoothAdapter的单例类,首先要用到它,即
   BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

   之后要判断设备是否支持蓝牙,可以这样判断
 
  1. BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); if(bluetooth != null) { }

   如果不为null,则支持

   还要判断用户是否启用蓝牙功能:
 
  1. if (bluetooth.isEnabled()) { } else{ }


4 接下来,我们显示个用户,如果启用了蓝牙,则显示蓝牙设备的名和状态
如果蓝牙设备没启用,也告诉用户
 
  1. String status; if (bluetooth.isEnabled()) { String mydeviceaddress = bluetooth.getAddress(); String mydevicename = bluetooth.getName(); status = mydevicename + ” : ” + mydeviceaddress; } else{ status = “Bluetooth is not Enabled.”; } Toast.makeText(this, status, Toast.LENGTH_LONG).show();


   这里使用getName()获得设备名,如果之前有打开
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

的话,可以这样设置蓝牙设备名:
  bluetooth.setName("AndroidCoder");
5 显示蓝牙状态:
   String state = bluetooth.getState(); status = mydevicename + ” : ” + mydeviceaddress + " : " + state;

  其中,蓝牙状态有:
  STATE_TURNING_ON
  STATE_ON
  STATE_TURNING_OFF
STATE_OFF
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/956223
推荐阅读
相关标签
  

闽ICP备14008679号