日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

04-iOS蓝牙传输数据演示

發布時間:2024/9/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 04-iOS蓝牙传输数据演示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

藍牙傳輸數據演示

  • 在上一小節中,我們一起開發了基于藍牙通訊的工具類,該類中詳細的實現藍牙連接流程中的每一個環節

  • 本小節我們就以給小米手環發送數據使其震動來演示我們工具類的用法

  • 工具類本身具有通用性,屬于MVC中的M層,只負責處理自身負責的處理,不處理任何的業務邏輯和UI

  • 我的小米手環的identifer:60C955B2-8F7C……

    • 后面我就不寫了,每一個手環的唯一標識符都是不一樣的
  • 能夠讓小米手環震動的特征的UUID:2A06
  • 能夠讓小米手環震動的數據:2(二進制數據)

  • 示例效果:1。點擊開始掃描按鈕,搜索藍牙設備,并且將外設的信息顯示在tableview中 2.點擊指定的tableviewcell,讓小米手環震動

#import "ViewController.h"#import "HMBluetoothManager.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView;@end//我的小米手環dentifier #define kIdentifier @"60C955B2-8F7C-8784-665F-D05E520F5A12"@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. }#pragma mark -掃描按鈕 - (IBAction)scanButtonClick:(id)sender {kHMBluetoothManager.UUID = @"2A06";//1.開始掃描[kHMBluetoothManager BeginScanPeripheral:^(CBPeripheral *peripheral) {//刷新tableview[self.tableView reloadData];}]; }#pragma mark -tableviewdelegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return kHMBluetoothManager.scanArr.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];CBPeripheral *peripheral = kHMBluetoothManager.scanArr[indexPath.row];cell.textLabel.text = [peripheral.identifier UUIDString];cell.detailTextLabel.text = peripheral.name;return cell; }//點擊cell連接設備 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {//獲取點擊的外設CBPeripheral *peripheral = kHMBluetoothManager.scanArr[indexPath.row];//判斷是否是我的小米手環(專門為了測試買的),因為藍牙會掃描到周邊很多外設,我們通過唯一標識符來判斷自己的外設if ([[peripheral.identifier UUIDString] isEqualToString:kIdentifier]) {//3.連接設備[kHMBluetoothManager connectPeripheral:peripheral Completion:^(CBPeripheral *peripheral, NSString *connectState) {NSLog(@"%@",connectState);//4.發送數據//實際開發中,掃描特征會有一定的延遲,我們可以通過回調或者通知來獲取發現特征的回調,這里為了快速演示,我就設置了3s的延遲dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{Byte *byte[1];byte[0]= 02 & 0xff;NSData *data = [NSData dataWithBytes:byte length:1];[kHMBluetoothManager writeValue:data toPeripheral:kHMBluetoothManager.currentPeripheral characteristic:kHMBluetoothManager.currentCharacteristic];});}];}else{NSLog(@"這不是你的小米手環");} }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的04-iOS蓝牙传输数据演示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。