1.Androidå¼åä¹èçï¼Bluetoothï¼
Androidå¼åä¹èçï¼Bluetoothï¼
å¨ä¸ä¸ç¯ä¸æä»ç»äºWifiä¸ç½ç»è¿æ¥å¤çAndroidå¼åä¹WiFiä¸ç½ç»è¿æ¥å¤ç
ä¸é¢ï¼æ¥ç»§ç»è¯´è¯´Androidä¸èççåºæ¬ä½¿ç¨ã
Bluetoothæ¯ç®å使ç¨çæ广æ³çæ 线é讯åè®®ä¹ä¸,傲世易支付系统源码主è¦é对çè·ç¦»è®¾å¤é讯ï¼ç±³ï¼ï¼å¸¸ç¨äºè¿æ¥è³æºãé¼ æ å移å¨é讯设å¤çã
å¼å¾ä¸æçæ¯ï¼
android4.2æ°å¢äºé¨åæ°åè½ï¼ä½æ¯å¯¹äºBluetoothçæç人æ许å¼å§å¤´ç¼äºï¼é£å°±æ¯Android4.2å¼å ¥äºä¸ä¸ªæ°çèçåè®®æ éBLEãè°·æåBroadcomä¹é´çåä½ï¼å¼åæ°çèçåè®®æ ï¼å代äºåºäºå æ çBluezãå æ¤å¸åºä¸åºç°äºè设å¤çå ¼å®¹é®é¢ï¼å¾å¤èç设å¤å¨android4.2ææºä¸ä¸è½æ£å¸¸ä½¿ç¨ã
BluetoothAdapterç®åç¹æ¥è¯´å°±æ¯ä»£è¡¨äºæ¬è®¾å¤(ææºãçµèç)çèçéé å¨å¯¹è±¡ã
firstï¼we need permission
è¦æä½èçï¼å è¦å¨AndroidManifest.xmléå å ¥æé
**ä¸é¢æ¥ççå¦ä½ä½¿ç¨èçã **âââ
****Demo已就绪ï¼
è¿åå¼ï¼å¦æ设å¤å ·å¤èçåè½ï¼è¿åBluetoothAdapter å®ä¾ï¼å¦åï¼è¿ånull对象ã
æå¼èç设å¤çæ¹å¼ï¼
1.ç´æ¥è°ç¨å½æ°enable()å»æå¼èçè®¾å¤ ï¼
2.ç³»ç»APIå»æå¼èç设å¤ï¼è¯¥æ¹å¼ä¼å¼¹åºä¸ä¸ªå¯¹è¯æ¡æ ·å¼çActivityä¾ç¨æ·éæ©æ¯å¦æå¼èç设å¤ã
注æï¼1.å¦æèçå·²ç»å¼å¯ï¼ä¸ä¼å¼¹åºè¯¥Activityçé¢ã2.å¨ç®å大å¤æ°Androidææºä¸ï¼æ¯ä¸æ¯æå¨é£è¡æ¨¡å¼ä¸å¼å¯èççãå¦æèçå·²ç»å¼å¯ï¼é£ä¹èççå¼å ³ ,ç¶æä¼éçé£è¡æ¨¡å¼çç¶æèåçæ¹åã
1. æç´¢èç设å¤
使ç¨BluetoothAdapterçstartDiscovery()æ¹æ³æ¥æç´¢èç设å¤
startDiscovery()æ¹æ³æ¯ä¸ä¸ªå¼æ¥æ¹æ³ï¼è°ç¨åä¼ç«å³è¿åã该æ¹æ³ä¼è¿è¡å¯¹å ¶ä»èç设å¤çæç´¢ï¼è¯¥è¿ç¨ä¼æç»ç§ã该æ¹æ³è°ç¨åï¼æç´¢è¿ç¨å®é ä¸æ¯å¨ä¸ä¸ªSystem Serviceä¸è¿è¡çï¼æ以å¯ä»¥è°ç¨cancelDiscovery()æ¹æ³æ¥åæ¢æç´¢ï¼è¯¥æ¹æ³å¯ä»¥å¨æªæ§è¡discovery请æ±æ¶è°ç¨ï¼ã
ç³»ç»å¼å§æç´¢èç设å¤
^( * ï¿£(oo)ï¿£ ) ^ ç³»ç»ä¼åé以ä¸ä¸ä¸ªå¹¿æï¼
2.æ«æ设å¤
3.å®ä¹å¹¿ææ¥æ¶å¨æ¥æ¶æç´¢ç»æ
4.注å广æ
è·åéè¿çèç设å¤
第ä¸æ¥å»ºç«è¿æ¥ï¼é¦å Android sdkï¼2.0以ä¸çæ¬ï¼æ¯æçèçè¿æ¥æ¯éè¿BluetoothSocket建ç«è¿æ¥ï¼æå¡ç«¯BluetoothServerSocketå客æ·ç«¯ï¼BluetoothSocketï¼éæå®åæ ·çUUIDï¼æè½å»ºç«è¿æ¥ï¼å 为建ç«è¿æ¥çæ¹æ³ä¼é»å¡çº¿ç¨ï¼æ以æå¡å¨ç«¯å客æ·ç«¯é½åºå¯å¨æ°çº¿ç¨è¿æ¥ã
ï¼è¿éçæå¡ç«¯å客æ·ç«¯æ¯ç¸å¯¹æ¥è¯´çï¼
两个èç设å¤ä¹é´çè¿æ¥ï¼åå¿ é¡»å®ç°æå¡ç«¯ä¸å®¢æ·ç«¯çæºå¶ã
å½ä¸¤ä¸ªè®¾å¤å¨åä¸ä¸ªRFCOMM channelä¸åå«æ¥æä¸ä¸ªè¿æ¥çBluetoothSocketï¼è¿ä¸¤ä¸ªè®¾å¤æå¯ä»¥è¯´æ¯å»ºç«äºè¿æ¥ã
æå¡ç«¯è®¾å¤ä¸å®¢æ·ç«¯è®¾å¤è·åBluetoothSocketçéå¾æ¯ä¸åçã
1ï¼æå¡ç«¯è®¾å¤æ¯éè¿acceptedä¸ä¸ªincoming connectionæ¥è·åçï¼
2ï¼å®¢æ·ç«¯è®¾å¤åæ¯éè¿æå¼ä¸ä¸ªå°æå¡ç«¯çRFCOMM channelæ¥è·åçã
æå¡ç«¯
éè¿è°ç¨BluetoothAdapterçlistenUsingRfcommWithServiceRecord(String, UUID)æ¹æ³æ¥è·åBluetoothServerSocketï¼UUIDç¨äºå®¢æ·ç«¯ä¸æå¡ç«¯ä¹é´çé 对ï¼
客æ·ç«¯
è°ç¨BluetoothServiceçcreateRfcommSocketToServiceRecord(UUID)æ¹æ³è·åBluetoothSocketï¼è¯¥UUIDåºè¯¥åäºæå¡ç«¯çUUIDï¼ã
è°ç¨BluetoothSocketçconnect()æ¹æ³ï¼è¯¥æ¹æ³ä¸ºblockæ¹æ³ï¼ï¼å¦æUUIDåæå¡ç«¯çUUIDå¹é ï¼å¹¶ä¸è¿æ¥è¢«æå¡ç«¯acceptï¼åconnect()æ¹æ³è¿åã
æ°æ®ä¼ éï¼éè¿ä»¥ä¸æä½ï¼å°±å·²ç»å»ºç«çBluetoothSocketè¿æ¥äºï¼æ°æ®ä¼ éæ éæ¯éè¿æµçå½¢å¼
è·åæµ
该类就æ¯å ³äºè¿ç¨èç设å¤çä¸ä¸ªæè¿°ãéè¿å®å¯ä»¥åæ¬å°èç设å¤---BluetoothAdapterè¿æ¥éä¿¡ã
好å¤ä¸è¥¿æä¹ä¸ç¥éæä¹æè¿°ï¼ä¸é¢ç»åºDemoï¼
å好æåå¦ä¹ çå°ä¼ä¼´é®æListViewæä¹ç¨ï¼é£æå°±ç¨ListViewã
æºç ï¼
RairDemo
GitHub: /Rairmmd/android-demo
Coding: /u/Rair/p/RairDemo/git