生活随笔
收集整理的這篇文章主要介紹了
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
#define kIdentifier @"60C955B2-8F7C-8784-665F-D05E520F5A12"@implementation ViewController- (
void)viewDidLoad {[
super viewDidLoad];
}
#pragma mark -掃描按鈕
- (
IBAction)scanButtonClick:(
id)sender {kHMBluetoothManager
.UUID = @
"2A06";[kHMBluetoothManager BeginScanPeripheral:^(CBPeripheral *peripheral) {[
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;
}
- (
void)tableView:(
UITableView *)tableView didSelectRowAtIndexPath:(
NSIndexPath *)indexPath
{CBPeripheral *peripheral = kHMBluetoothManager
.scanArr[indexPath
.row];
if ([[peripheral
.identifier UUIDString] isEqualToString:kIdentifier]) {[kHMBluetoothManager connectPeripheral:peripheral Completion:^(CBPeripheral *peripheral,
NSString *connectState) {
NSLog(@
"%@",connectState);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];
}
@end
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的04-iOS蓝牙传输数据演示的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。