1:权限
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2:打开蓝牙
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
finish();
} else if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
}
3:注册广播以及扫描
// 开始扫描
bluetoothAdapter.startDiscovery();
/*** 注册广播**/
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver,filter);
IntentFilter filter2=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver,filter2);
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == BluetoothDevice.ACTION_FOUND) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
String s = device.getName() + "*" + device.getAddress();
if(device.getAddress().equals(blueId)){
bluetoothDevice=device;
Log.d(TAG, "onReceive: --空2");
}
datas.add(device.getName() + "*" + device.getAddress());
Log.d(TAG, "onReceive: ---"+device.getName()+"---"+device.getAddress());
}else if(device.getBondState()!=BluetoothDevice.BOND_BONDED){
String s=device.getName()+"*"+device.getAddress();
Log.d(TAG, "onReceive: ---"+device.getName()+"---"+device.getAddress());
if(device.getAddress().equals(blueId)){
bluetoothDevice=device;
Log.d(TAG, "onReceive: --空1");
}
if(datas.indexOf(s)==-1){
datas.add(device.getName()+"*"+device.getAddress());
}
}
}else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
Log.d(TAG, "onReceive: ---搜索完成");
Toast.makeText(MainActivity.this,"搜索完成!",Toast.LENGTH_SHORT).show();
}
}
};
4:配对
/***传入的参数是 需要配对的bluetoothdevice */
public void blePairing(BluetoothDevice bluetoothDevice){
this.bluetoothDevice=bluetoothDevice;
Method m = null;
try {
m = bluetoothDevice.getClass().getDeclaredMethod("createBond",new Class[]{});
m.setAccessible(true);
Boolean originalResult = null;
try {
originalResult = (Boolean) m.invoke(bluetoothDevice);
boolean result = originalResult.booleanValue();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
5:连接
连接 connectA2dp(bluetoothDevice);
/**
* 连接 音箱device
* @param bluetoothDevice
*/
private void connectA2dp(BluetoothDevice bluetoothDevice) {
if (bluetoothA2dp == null || bluetoothDevice == null) {
return;
}
setPriority(bluetoothDevice, 100);
try {
Method connectMethod = BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class);
connectMethod.invoke(bluetoothA2dp, bluetoothDevice);
} catch (Exception e) {
e.printStackTrace();
}
}
6: 连接需要的BluetoothA2dp对象需要通过监听得到
/***
* blueA2dp 监听
*/
private BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
if(profile == BluetoothProfile.A2DP){
bluetoothA2dp = null;
}
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if(profile == BluetoothProfile.A2DP){
bluetoothA2dp = (BluetoothA2dp) proxy;
}
}
};
7:code
http://pan.baidu.com/s/1bBuYPS