1.android藍(lán)牙的幾個(gè)基本概念
- 權(quán)限
<uses-permissionandroid:name="[Android](http://lib.csdn.net/base/android).permission.BLUETOOTH" />
允許程序連接到已配對(duì)的藍(lán)牙設(shè)備问拘。
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
允許程序發(fā)現(xiàn)和配對(duì)藍(lán)牙設(shè)備铁材。
- 操作藍(lán)牙主要用到的類 BluetoothAdapter類,使用時(shí)導(dǎo)包
import android.bluetooth.BluetoothAdapter;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- 開啟藍(lán)牙
if(!mBluetoothAdapter.isEnabled()){
//彈出對(duì)話框提示用戶是后打開
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, REQUEST_ENABLE);
//不做提示栈妆,直接打開,不建議用下面的方法,有的手機(jī)會(huì)有問題嘁信。
// mBluetoothAdapter.enable();
}
- 獲取本地藍(lán)牙信息
//獲取本機(jī)藍(lán)牙名稱
String name = mBluetoothAdapter.getName();
//獲取本機(jī)藍(lán)牙地址
String address = mBluetoothAdapter.getAddress();
Log.d(TAG,"bluetooth name ="+name+" address ="+address);
//獲取已配對(duì)藍(lán)牙設(shè)備
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
Log.d(TAG, "bonded device size ="+devices.size());
for(BluetoothDevice bonddevice:devices){
Log.d(TAG, "bonded device name ="+bonddevice.getName()+" address"+bonddevice.getAddress());
}
- 搜索設(shè)備:
mBluetoothAdapter.startDiscovery();
開始搜索設(shè)備勤哗,該過程是異步的抡爹,通過下面注冊(cè)廣播接受者,可以監(jiān)聽是否搜到設(shè)備芒划。
IntentFilter filter = new IntentFilter();
//發(fā)現(xiàn)設(shè)備
filter.addAction(BluetoothDevice.ACTION_FOUND);
//設(shè)備連接狀態(tài)改變
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
//藍(lán)牙設(shè)備狀態(tài)改變
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBluetoothReceiver, filter);
- 監(jiān)聽掃描結(jié)果
private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG,"mBluetoothReceiver action ="+action);
if(BluetoothDevice.ACTION_FOUND.equals(action)){//每掃描到一個(gè)設(shè)備冬竟,系統(tǒng)都會(huì)發(fā)送此廣播。
//獲取藍(lán)牙設(shè)備
BluetoothDevice scanDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(scanDevice == null || scanDevice.getName() == null) return;
Log.d(TAG, "name="+scanDevice.getName()+"address="+scanDevice.getAddress());
//藍(lán)牙設(shè)備名稱
String name = scanDevice.getName();
if(name != null && name.equals(VnApplication.BLUETOOTH_NAME)){
mBluetoothAdapter.cancelDiscovery();
//取消掃描
mProgressDialog.setTitle(getResources().getString(R.string.progress_connecting)); //連接到設(shè)備民逼。
mBlthChatUtil.connect(scanDevice);
}
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
}
}
};
- 連接:
此方法中主要是獲取socket客戶端诱咏,并連接。
BluetoothSocket mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
mmSocket.connect();
- 連接成功后缴挖,獲取輸入輸出流袋狞,進(jìn)行通信
當(dāng)連接成功后,connected(mmSocket, mmDevice); 獲取輸入輸出流映屋,從而進(jìn)行通信苟鸯。
private InputStream mmInStream = socket.getInputStream();
private OutputStream mmOutStream =socket.getOutputStream();
mmOutStream.write(buffer);發(fā)送信息。
mmInStream.read(buffer); 收到消息棚点。