android 蓝牙功能
16lz
2021-01-23
public class MainActivity extends Activity{ int REQUEST_ENABLE_BT = 1; …… @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initBluetooth(); } /*判断手机是否已经打开蓝牙,如果没有,询问用户是否要打开*/ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public void initBluetooth() { //如果手机硬件不支持蓝牙,弹出提示消息 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, "Not support BLE", Toast.LENGTH_SHORT).show(); // finish(); } //获取手机本地的蓝牙适配器 final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); //如果蓝牙没打开,询问用户是否要打开蓝牙 if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }}
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //获取已配对的蓝牙设备 Set devices = adapter.getBondedDevices(); for (BluetoothDevice device : devices) { System.out.println("已配对的设备名称:" + device.getName() + "uuids" + device.getUuids()); }
更多相关文章
- 获取Android 手机屏幕宽度和高度以及获取Android手机序列号
- Android使用系统分享文件给微信,QQ指定的用户
- Android 蓝牙开发浅析
- Android加速度传感器实现“摇一摇”,带手机振动
- Android 手机存储文件各种路径
- android:向手机卡上写入文件时总是不成功,解决办法
- Android SQLiteOpenHelper(手机数据库)
- Android利用WebService查询手机号码归属地
- Ubuntu 10.10 与 华为 C8650 手机搭建 Android 真机开发调试环境