1.Android èçå¼å(ä¸)-èçç详ç»ä»ç»
2.Android èçå¼å(ä¸)
3.Androidå¼åä¹èçï¼Bluetoothï¼
Android èçå¼å(ä¸)-èçç详ç»ä»ç»
åé¢ç两ç¯æç« ï¼ä¸»è¦æ¯å¨ Android å®ç½å ³äºèçä»ç»çåºç¡ä¸å ä¸èªå·±çç解å®æçã主è¦é对çæ¯ Android å¼åä¸çä¸äº API ç使ç¨ã第ä¸ç¯æç« Android èçå¼åï¼ä¸ï¼ 主è¦æ¯ä»ç»äºæ®éçèçå¨ Android å¼åä¸çè¿ç¨ã
第äºç¯æç« Android èçå¼åï¼äºï¼ 主è¦æ¯ä»ç»äºä½åèèççå¼åã
è¿ç¯æç« ä¸»è¦ä»ç»çæ¯èççåå²åä¸äºå ³äºèççéç¨ç¥è¯ï¼è¿æ广æå çç¥è¯ãè¦æ³å½»åºäºè§£èçå¼åï¼è¿äºåºç¡çç¥è¯ä¹æ¯éè¦çï¼å°±åç½ç»åè®®ä¸æ ·ï¼è¿äºé½æ¯åºç¡çå 容ãæ们ç API çè°ç¨é½æ¯ä»¥è¿ä¸ªä¸ºåºç¡çï¼äºè§£è¿äºï¼å¼åè¿ç¨ä¸éå°é®é¢ï¼æå¯ä»¥ç¥éä»ä¹æä¹ä¸åäºã
ä¸ç¯æç« ä¸»è¦è®²çå°±æ¯å®é å¼åä¸çä¸äºåã
èçå ¶å®å°±æ¯ä¸ç§è¿è·ç¦»æ 线éä¿¡ææ¯ã
ä»ä¸å°ä¸åå«ä¸ºï¼æ§å¶å¨ï¼Controllerï¼-->主æºï¼hostï¼-->åºç¨ï¼Applicationï¼
详ç»ä»ç»å个å±çå«ä¹ï¼
BLE åºç¨å¯ä»¥å为两大类ï¼åºäºéè¿æ¥çååºäºè¿æ¥ç
ææå°±æ¯å¤è®¾åå¨è¾¹è®¾å¤ä¸åçè¿æ¥ï¼ä¸»è¦é æ«æå°ç广ææ¥è·åä¿¡æ¯ãåé广æçä¸æ¹å«å broadcaster çå¬å¹¿æçä¸æ¹å«å oberver å¨ GAP å±æ对åºçè§è²å®ä¹ã
ç½ç»ææå¾ï¼
è¿ç§æ¹å¼å°±æ¯å¹¿æ设å¤ä¸æçåå¤åé广æï¼å«æç¹å®çä¿¡æ¯ï¼ï¼ç¶åè§å¯è æ¥åå°å¹¿ææç §ä¸¤è ä¹é´çº¦å®å¥½çåè®®è¿è¡è§£ææ¿å°æç¨çä¿¡æ¯ãä¾å¦ï¼iBeaconï¼éè¿è¿ç§è®¾å¤æ们å¯ä»¥å®ç°å®¤å å®ä½ã
å ¶å®è¿äºè®¾å¤çè§è²å¯ä»¥å³ä½¿å¹¿æè åæ¯è§å¯è ãæ¥æ¶å°å¹¿æåä½åºäºå¤çï¼ç¶åååé广æãè¿æ ·å°±å½¢æäºååçç½ç»ï¼ç±»ä¼¼äºå ç¹ç½ï¼è¿å°±æ¯èç Mesh ç»ç½ã
广ææ°æ®å æ ¼å¼ï¼
æ¯ä¸ªå¹¿ææ°æ®å ç± byte ç»æãå为æææ°æ®åæ ææ°æ®ä¸¤é¨åã
ä¾åï¼
è¿éæ¯æ«æçæ°æ®å ï¼è½¬æ¢æäº è¿å¶ï¼ä¸¤ä¸ªä»£è¡¨ä¸ä¸ªåèï¼ï¼ç¬¬ä¸ä¸ªåèæ¯ è¡¨ç¤ºåé¢ç两个åèæ¯æ°æ®é¨åï¼ç¶å第äºä¸ªåèæ¯ è¡¨ç¤ºäºæ°æ®çç±»åãåé¢ä¸ä¸ªåèå°±æ¯çæ£çæ°æ®äºãè¿ä¸ªå¹¿ææ°æ®åå å°±åæå®äºãä¸é¢å°±æ¯å¦ä¸ä¸ªæ°æ®åå äºãä¾æ¬¡ç±»æ¨ï¼å ³äºæ°æ®ç±»åç解éï¼å®ç½æã
è¿æ¯æ°æ®ç±»å对åºçå«ä¹è¡¨ã
ç½ç»ææå¾ï¼
ä¸ä¸ªä¸å¿è®¾å¤å¯è¿æ¥å¤ä¸ªå¤è®¾ï¼ä½æ¯ä¸ä¸ªå¤è®¾åªè½è¿æ¥ä¸ä¸ªä¸å¿ï¼å¤è®¾è¿æ¥æååå°±ä¼åæ¢å¯¹å¤å¹¿æï¼å«äººå°±åç°ä¸äºå®äºï¼ãå ¶ä¸ä¸ä¸ªä¸å¿è®¾å¤çè¿æ¥å¤è®¾çæ°éä¹æ¯æéçã
é¾æ¥ï¼ /s?牙开牙开波段雷达源码__biz=MzU5NzA2NjQzMg==&mid=&idx=1&sn=fd7ebceafeb4cf&scene=#wechat_redirect
Android èçå¼å(ä¸)
æ®éèç设å¤å®æ¹ææ¡£Android å¹³å°å å«èçç½ç»å æ æ¯æ ï¼ååæ¤æ¯æï¼è®¾å¤è½ä»¥æ 线æ¹å¼ä¸å ¶ä»èç设å¤äº¤æ¢æ°æ®ãåºç¨æ¡æ¶æä¾äºéè¿ Android Bluetooth API 访é®èçåè½çéå¾ãä½¿ç¨ Bluetooth API Android åºç¨å¯ä»¥æ§è¡ä¸é¢çæä½ï¼
ä¼ ç»èçéç¨äºçµæ± 使ç¨å¼ºåº¦è¾å¤§çæä½ï¼ä¾å¦ Android 设å¤ä¹é´çæµä¼ è¾åéä¿¡çãéå¯¹å ·æä½åèè¦æ±çèç设å¤ï¼Android 4.3ï¼API ï¼ä¸å¼å ¥äºé¢åä½åèèçç API æ¯æã
ä½¿ç¨ Android Bluetooth API æ¥å®æ使ç¨èçè¿è¡éä¿¡çå项主è¦ä»»å¡ï¼ 设置èç ã æ¥æ¾å±é¨åºåå çé 对设å¤æå¯ç¨è®¾å¤ ã è¿æ¥è®¾å¤ ï¼ä»¥åå¨ è®¾å¤ä¹é´ä¼ è¾æ°æ® ã
å ³äºèçç API å¨ android.bluetooth å ä¸ï¼ä¸é¢ä»ç»ä¸ä¸åèçç¸å ³ç主è¦ç±»:
å¨ BluetoothProfile IPC 客æ·ç«¯è¿æ¥å°æå¡ï¼å³ï¼è¿è¡ç¹å®é ç½®æ件çå é¨æå¡ï¼ææå¼æå¡è¿æ¥æ¶åå ¶åééç¥çæ¥å£ã
使ç¨èçå¿ é¡»å£°ææé BLUETOOTH æå¯ä»¥æ§è¡èçéä¿¡ã
1ãè·åèçéé å¨
ä¾å¦ï¼æ们å¯ä»¥æ¥è¯¢ææå·²é 对ç设å¤ï¼ç¶åä½¿ç¨ ArrayAdapter åç¨æ·æ¾ç¤ºæ¯å°è®¾å¤çå称ï¼
è¦åèµ·è¿æ¥ä» éè¦ç¥éç®æ èç设å¤ç Mac å°åå°±å¯ä»¥äºã
注ææ§è¡ discovery 对äºèçéé å¨æ¥è¯´æ¯ä¸ä¸ªé常ç¹éçè¿ç¨ï¼å¹¶ä¸ä¼æ¶è大éèµæºãå¨æ¾å°è¦è¿æ¥ç设å¤åï¼ è¦ç¡®ä¿ä½¿ç¨ cancelDiscovery() æ¥åæ¢åç°ï¼ç¶åå°è¯è¿æ¥ ãå¦ææ¨å·²ç»åæå°è®¾å¤è¿è¡è¿æ¥ï¼é£ä¹è¿ä¸ªæ¶åæ§è¡åç°æä½ä¼å¤§å¹ 度çåå°æ¤è¿æ¥å¯ç¨ç带宽ï¼å æ¤ä¸åºè¯¥å¨å¤äºè¿æ¥ç¶æçæ¶åæ§è¡åç°æä½ï¼
ä¾å¦ï¼
å¨è¿æ¥ä¹åå¦æ两个设å¤æ²¡æé 对ï¼åç³»ç»ä¼èªå¨ååºé 对请æ±ã
æå¡å¨å¥æ¥åæ¥åè¿æ¥çåºæ¬è¿ç¨
æ¾å¨å线ç¨ä¸å»æ§è¡ã
ä¾åï¼
客æ·ç«¯è¿æ¥çåºæ¬è¿ç¨
è°ç¨ connect() çæ¶åè¦ç¡®ä¿å®¢æ·ç«¯æ²¡ææ§è¡åç°æä½ãå¦ææ§è¡äºä¼å¤§å¹ 度éä½è¿æ¥çé度ï¼å¢å 失败çå¯è½ã
ä¾å
å¨è¿æ¥ä¹åè°ç¨ cancleDiscovery() å¨è¿è¡è¿æ¥ä¹ååºè¯¥å§ç»è°ç¨è¿ä¸ªæ¹æ³ï¼èä¸è°ç¨çæ¶åæ éæ£æµæ¯å¦æ£å¨æ«æã
è¿ç¨ï¼
ä» Android 3.0 å¼å§ï¼ Bluetooth API 便æ¯æ使ç¨èçé ç½®æ件ãèçé ç½®æ件æ¯éç¨äºè®¾å¤é´èçéä¿¡çæ 线æ¥å£è§èã
1ãèçé ç½®æ件就æ¯è®¾å¤é´éä¿¡ï¼èç设å¤ï¼çä¸ç§è§è
å æé ç½®æ件便æ¯ä¸ä¸ªç¤ºä¾ï¼å¯¹äºè¿æ¥å°æ 线è³æºçææºï¼ä¸¤å°è®¾å¤é½å¿ é¡»æ¯æå æé ç½®æ件ãæ们ä¹å¯ä»¥éè¿å®ç°æ¥å£ BluetoothProfile æ¥åå ¥èªå·±çç±»æ¥æ¯æç¹å®çèçé ç½®æ件ãAndroid API æä¾äºä»¥ä¸çå ç§èçé ç½®æ件çå®ç°ï¼
2ã使ç¨é ç½®æ件çåºæ¬æ¥éª¤
å建 HDP åºç¨ï¼
å ³äºæ®éèç设å¤åæ®éèç设å¤ä¹é´çè¿æ¥éä¿¡
å ³äºèç设å¤åèç仪å¨ï¼èçè³æºãçµå秤çç类似产åï¼
è¿ç§ä¹é´çéä¿¡æ¯éè¿é ç½®æ件代çæ¥å®ç°çã
é½æä¸ä¸ªå¯¹åºçé ç½®æ件代çç±»ãå ·ä½çæä½æ¯éè¿è¿ä¸ªå¯¹è±¡æ¥å®æã
åèï¼ /s?__biz=MzU5NzA2NjQzMg==&mid=&idx=1&sn=9db9adfbbcc&scene=#wechat_redirect
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