iOS 蓝牙开发 Mac地址问题
<p>在蓝牙开发中,经常会遇到蓝牙<span style="color: rgba(255, 0, 0, 1)">Mac地址</span>(<span style="color: rgba(255, 0, 0, 1)">物理地址</span>-->唯一标记)链接的问题,</p><p>对于<span style="color: rgba(255, 0, 0, 1)">安卓</span>来说,可以通过Mac地址来链接,因为他们<span style="color: rgba(255, 0, 0, 1)">可以获取</span>到外设的Mac地址。</p>
<p>对于<span style="color: rgba(255, 0, 0, 1)">苹果</span>而言,是不可以通过Mac地址链接的,因为我们<span style="color: rgba(255, 0, 0, 1)">无法获取</span>外设的Mac地址。</p>
<p>虽然苹果给我们提供了一个<span style="color: rgba(255, 0, 0, 1)">外设的UUID</span>,但是这个UUID是通过<span style="color: rgba(255, 0, 0, 1)">外设的Mac地址</span>和<span style="color: rgba(255, 0, 0, 1)">手机的Mac地址</span>进行加密计算得来的。</p>
<p>换言之,<span style="color: rgba(255, 0, 0, 1)">不同手机链接同一外设这个值是不同的</span>,所以这个值并不能取代Mac地址作为唯一标记,况且安卓是没有这个UUID的。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(255, 0, 0, 1)">peripheral.identifier.UUIDString</span>;</pre>
</div>
<div class="cnblogs_code">
<pre>*!
*<span style="color: rgba(0, 0, 0, 1)">@property identifier
</span>*
*<span style="color: rgba(0, 0, 0, 1)">@discussion The unique, persistent identifier associated with the peer.
</span>*/<span style="color: rgba(0, 0, 0, 1)">
@property(</span><span style="color: rgba(0, 0, 255, 1)">readonly</span>, nonatomic) NSUUID *identifier NS_AVAILABLE(10_13, 7_0);</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Return a string description of the UUID, such as "E621E1F8-C36C-495A-93FC-0C247A3E6E5F" </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@property (</span><span style="color: rgba(0, 0, 255, 1)">readonly</span>, copy) NSString *UUIDString;</pre>
</div>
<p> </p>
<p><span style="font-family: 黑体; font-size: 14pt">那i<span style="color: rgba(255, 0, 0, 1)">OS如何获取Mac地址</span>呢?</span></p>
<p><span style="color: rgba(255, 0, 0, 1)">方案A:</span></p>
<p>硬件工程师将Mac地址写到<span style="color: rgba(255, 0, 0, 1)">厂商数据</span>中,然后我们获取(<span style="color: rgba(255, 0, 0, 1)">扫描阶段</span>就可以获取到)</p>
<p> </p>
<p><span style="color: rgba(255, 0, 0, 1)">方案B</span>:(本质就是读取固定指令获取)</p>
<p>读取外设的<span style="color: rgba(255, 0, 0, 1)">180A服务里的2A23特征值</span>,再去进行剪切和拼接来获取(必须<span style="color: rgba(255, 0, 0, 1)">先链接上</span>外设)</p>
<p> </p>
<p>详情参考:https://www.jianshu.com/p/1d6a8fc8134f </p>
<p>但这个方案有<span style="color: rgba(255, 0, 0, 1)">大弊端</span>:</p>
<p>1、就是我必须先链接上然后再去获取这个Mac地址,<span style="color: rgba(255, 0, 0, 1)">太消耗成本</span>,且效率不高。</p>
<p>假如有很多设备的话,我必须一一验证,不是再断开,远没有在扫描阶段(扫描的过程是很快的,可以在短短几秒钟内就可以找到周围的上百台设备),确认是再链接高效。</p>
<p>2、这样会有<span style="color: rgba(255, 0, 0, 1)">莫名其妙的问题</span>。</p>
<p>3、从<span style="color: rgba(255, 0, 0, 1)">链接</span>的角度来讲有点<span style="color: rgba(255, 0, 0, 1)">本末倒置</span>,本来我是用Mac地址来链接的,但现在变成了,先链接再获取Mac地址,验证是否正确。</p>
<p>4、仅限<span style="color: rgba(255, 0, 0, 1)">某些设备</span>,如果你的蓝牙设备不支持这样获取,你需要跟硬件工程师沟通,说白了并<span style="color: rgba(255, 0, 0, 1)">不通用</span></p>
<p> </p>
<p><span style="font-family: 黑体; font-size: 14pt">那<span style="color: rgba(255, 0, 0, 1)">iOS如何链接</span>呢?</span></p>
<p>一般我们可以通过<span style="color: rgba(255, 0, 0, 1)">名称、厂商数据</span>来链接。</p>
<p>当手机蓝牙(此地将手机蓝牙作为中心设备)扫描到外设(譬如手环之类的)后,会进入这个方法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">!
*@method centralManager:didDiscoverPeripheral:advertisementData:RSSI:
*
*@param central The central manager providing this update.
*@param peripheral A <code>CBPeripheral</code> object.
*@param advertisementData A dictionary containing any advertisement and scan response data.
*@param RSSI The current RSSI of <i>peripheral</i>, in dBm. A value of <code>127</code> is reserved and indicates the RSSI
* was not available.
*
*@discussion This method is invoked while scanning, upon the discovery of <i>peripheral</i> by <i>central</i>. A discovered peripheral must
* be retained in order to use it; otherwise, it is assumed to not be of interest and will be cleaned up by the central manager. For
* a list of <i>advertisementData</i> keys, see {@link CBAdvertisementDataLocalNameKey} and other similar constants.
*
*@seealso CBAdvertisementData.h
*
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, <span style="color: rgba(0, 0, 255, 1)">id</span>> *)advertisementData RSSI:(NSNumber *)RSSI;</pre>
</div>
<p>名称、厂商数据可以从<span style="color: rgba(255, 0, 0, 1)">advertisementData</span>(广告数据)中读取</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">!
*@constant CBAdvertisementDataLocalNameKey
*
*@discussion A <code>NSString</code> containing the local name of a peripheral.
*
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
CB_EXTERN NSString </span>* <span style="color: rgba(0, 0, 255, 1)">const</span> CBAdvertisementDataLocalNameKey;</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">!
*@constant CBAdvertisementDataManufacturerDataKey
*
*@discussion A <code>NSData</code> object containing the manufacturer data of a peripheral.
*
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
CB_EXTERN NSString </span>* <span style="color: rgba(0, 0, 255, 1)">const</span> CBAdvertisementDataManufacturerDataKey;</pre>
</div>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">advertisementData中可以发送的数据有约定 如下</span>
<span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
18 对应设置NSString类型的广播名
19 NSString *const CBAdvertisementDataLocalNameKey;
20 外设制造商的NSData数据
21 NSString *const CBAdvertisementDataManufacturerDataKey;
22 外设制造商的CBUUID数据
23 NSString *const CBAdvertisementDataServiceDataKey;
24 服务的UUID与其对应的服务数据字典数组
25 NSString *const CBAdvertisementDataServiceUUIDsKey;
26 附加服务的UUID数组
27 NSString *const CBAdvertisementDataOverflowServiceUUIDsKey;
28 外设的发送功率 NSNumber类型
29 NSString *const CBAdvertisementDataTxPowerLevelKey;
30 外设是否可以连接
31 NSString *const CBAdvertisementDataIsConnectable;
32 服务的UUID数组
33 NSString *const CBAdvertisementDataSolicitedServiceUUIDsKey;
34 </span><span style="color: rgba(0, 128, 0, 1)">*/</span></pre>
</div>
<p> </p>
<p>具体如何保证<span style="color: rgba(255, 0, 0, 1)">唯一性</span>,这个就需要和蓝牙硬件工程师约定了。</p>
<p><span style="color: rgba(255, 0, 0, 1)">1</span>、如果名称是唯一的,我们就可以用名称来约定唯一性。</p>
<p><span style="color: rgba(255, 0, 0, 1)">2</span>、如果名称相同,又要确定唯一性,那我们就需要通过厂商数据来约定了。</p>
<p>具体这个规则要怎么来约定唯一性,就靠彼此约定了,这个规则随意,只要彼此双方约定好就可以。</p>
<p> </p>
<p>综上:</p>
<p>比较好的<span style="font-family: 黑体; font-size: 18px; color: rgba(255, 0, 0, 1)">链接方案</span>就是,<span style="font-family: 黑体; font-size: 18px; color: rgba(255, 0, 0, 1)">苹果、安卓、硬件</span>大家约定好一个<span style="font-size: 18px">规则</span>,不论是通过<span style="font-size: 18px; color: rgba(255, 0, 0, 1)">名称或者厂商数据</span>都可以确定<span style="font-size: 18px; color: rgba(255, 0, 0, 1)">唯一性通用性</span>。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/lijianyi/p/14761020.html
頁:
[1]