Android中蓝牙使用步骤小结
下面小结下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();
之后要判断设备是否支持蓝牙,可以这样判断
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); if(bluetooth != null) { } if (bluetooth.isEnabled()) { } else{ }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();