最近對Android設備的藍牙操做進行了一些研究, 下面作一些總結, 版本是4.4,列出的解決方案多來源於網絡,感謝強大的網友們:html
操做藍牙能夠分爲常規的操做,和很是規的操做。所謂常規的操做,就是界面上有提示,須要客戶許可進行的一些操做。很是規的則一般是採用反射等手段,達到不知不覺鏈接藍牙的目的。android
一. 常規操做:網絡
1. 獲取藍牙的操做接口:socket
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
藍牙的相關操做基本都是經過上面這個類。ide
2. 打開本機的藍牙設備:this
if (!mBtAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 0);
}
3. 打開藍牙的可見性:spa
if (mBtAdapter.isEnabled()) { Intent visibleIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); visibleIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); // 後面的ms最多就是300 startActivity(visibleIntent); }
4. 開啓了藍牙設備,就是爲了與其餘設備通訊,因此須要掃描周圍可用的設備:.net
// 註冊兩個intent,並定義receiver接受藍牙設備檢測過程當中,"發現設備"和"完成"的回調事件
IntentFilter discoveryFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
IntentFilter discoveryFinishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
BroadcastReceiver discoverReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 拿到別的device才能進行鏈接操做
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
// DO Something
}
}
};
this.registerReceiver(discoverReceiver, discoveryFilter);
this.registerReceiver(discoverReceiver, discoveryFinishedFilter);
/************************************/
mBtAdapter.startDiscovery(); // 用來開始搜索周圍可見的藍牙設備 // 若是在發現過程當中想要中止,能夠調用下面的API if(mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); return; }
5. 做爲server端,等待其餘設備鏈接:線程
BluetoothServerSocket mServerSocket = mBtAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
BluetoothSocket mClientSocket = mServerSocket.accept();
// 有了socket對象,能夠獲取stream了, 能夠用stream的read,write方法來讀寫數據了,read能夠在獨立線程的循環裏,以保證持續接受到數據。
InputStream inStream = mClientSocket.getInputStream();
OutputStream outStream = mClientSocket.getOutputStream();
6. 做爲client端,能夠去鏈接server:code
BluetoothDevice mDevice = mBtAdapter.getRemoteDevice(device.getAddress()); //首先要得到server的設備對象,這個兌現能夠在以前 discover的時候就拿到,也能夠經過以前記錄的address獲取到
// connect 若是成功了,就獲得socket鏈接了,以後和上面同樣,就能夠經過stream進而收發消息了
mClientSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mClientSocket.connect();
二. 很是規操做:
1. 打開本機的藍牙設備:
// 這樣是不會彈出對話框的
if (!mBtAdapter.isEnabled()) { mBtAdapter.enable(); }
2. 打開藍牙的可見性:
//值得一提的是,這個方法在 mBtAdapter.enable(); 以後當即調用是頗有可能不生效的。多是藍牙設備開啓須要一些時間。因此,最好是在確認設備確實已經開啓了以後(好比說sleep一下子,或者有個有個循環不斷check mBtAdapter.isEnabled (),來保證設備已經準備好了。
public void setDiscoverableTimeout(int timeout) {
try {
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class); setDiscoverableTimeout.setAccessible(true); Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class); setScanMode.setAccessible(true); setDiscoverableTimeout.invoke(mBtAdapter, timeout); setScanMode.invoke(mBtAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout); } catch (Exception e) { e.printStackTrace(); } }
3. 不配對就進行藍牙通訊:
// 經過這兩個API, 能夠很神奇的, 不配對藍牙設備就進行通訊
mServerSocket = mBtAdapter.listenUsingInsecureRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mClientSocket = mDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
三. 最後列一下須要的權限:
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
藍牙參考網頁:
http://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android
http://stackoverflow.com/questions/5885438/bluetooth-pairing-without-user-confirmation
http://blog.csdn.net/zshq280017423/article/details/7645622
http://blog.csdn.net/menghnhhuan/article/details/7057484
http://blog.csdn.net/eric41050808/article/details/16967189
http://www.2cto.com/kf/201312/261093.html
WIFI 參考網頁:
http://blog.csdn.net/ranger1111/article/details/6777153
http://lszdb1983.blog.163.com/blog/static/20426348201209251344/
格式啊,怎麼也搞很差,,,