当前位置: 首页 > 编程日记 > 正文

iOS蓝牙4.0开发例子

1建立中心角色

1
2
3
#import <CoreBluetooth/CoreBluetooth.h> 
CBCentralManager *manager; 
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 

2扫描外设(discover)

[manager scanForPeripheralsWithServices:nil options:options]; 

3连接外设(connect)

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
{
        if([peripheral.name  isEqualToString:BLE_SERVICE_NAME]){
                [self connect:peripheral];
        }
s); 
}       
 
-(BOOL)connect:(CBPeripheral *)peripheral{
        self.manager.delegate = self;
        [self.manager connectPeripheral:peripheral
                                options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}

  

4扫描外设中的服务和特征(discover)

1
2
3
4
5
6
7
8
9
10
11
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
       
    NSLog(@"Did connect to peripheral: %@", peripheral); 
    _testPeripheral = peripheral; 
       
    [peripheral setDelegate:self];  <br>//查找服务
    [peripheral discoverServices:nil]; 
       
       
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error 
   
       
    NSLog(@"didDiscoverServices"); 
       
    if (error) 
    
        NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); 
           
        if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)]) 
            [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil]; 
           
        return
    
       
   
    for (CBService *service in peripheral.services) 
    
         //发现服务
        if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]]) 
        
            NSLog(@"Service found with UUID: %@", service.UUID);  <br>//查找特征
            [peripheral discoverCharacteristics:nil forService:service]; 
            break
        
           
           
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
     
    if (error)
    {
        NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
         
        [self error];
        return;
    }
     
    NSLog(@"服务:%@",service.UUID);
    for (CBCharacteristic *characteristic in service.characteristics)
    {
       //发现特征
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {
                NSLog(@"监听:%@",characteristic);<br>//监听特征
                [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
            }
         
    }
}

5与外设做数据交互(读 与 写)  

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if (error)
    {
        NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]);
        self.error_b = BluetoothError_System;
        [self error];
        return;
    }
     
//    NSLog(@"收到的数据:%@",characteristic.value);
    [self decodeData:characteristic.value];
}

1
2
NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];
                [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse

转载于:https://www.cnblogs.com/Free-Thinker/p/5081209.html

相关文章:

Spark Shuffle原理解析

Spark Shuffle原理解析 一&#xff1a;到底什么是Shuffle&#xff1f; Shuffle中文翻译为“洗牌”&#xff0c;需要Shuffle的关键性原因是某种具有共同特征的数据需要最终汇聚到一个计算节点上进行计算。 二&#xff1a;Shuffle可能面临的问题&#xff1f;运行Task的时候才会产…

云开发使用 got 的 get/post 传参请求示例代码

使用 got 进行网络请求的步骤&#xff1a; 1.创建云函数&#xff0c;并在终端执行云函数 2.执行 npm 安装 got &#xff0c;命令&#xff1a;cnpm install --save got 3.在云函数中使用 示例代码&#xff1a; // 云函数入口文件 const cloud require(wx-server-sdk) cons…

JavaScript词法作用域的简单介绍

by Michael McMillan迈克尔麦克米兰(Michael McMillan) JavaScript词法作用域的简单介绍 (An easy intro to Lexical Scoping in JavaScript) Lexical scoping is a topic that frightens many programmers. One of the best explanations of lexical scoping can be found in…

Flex 布局详解 - 转自阮一峰老师

Flex 是 Flexible Box 的缩写&#xff0c;意为"弹性布局"&#xff0c;用来为盒状模型提供最大的灵活性。任何的盒子都可以使用它。 下面我们来看一下使用 Flex 布局的容器的属性 flex-direction flex-wrap flex-flow justify-content align-items align-content 1.…

bzoj 1086: [SCOI2005]王室联邦

Description “余”人国的国王想重新编制他的国家。他想把他的国家划分成若干个省&#xff0c;每个省都由他们王室联邦的一个成员来管理。他的国家有n个城市&#xff0c;编号为1..n。一些城市之间有道路相连&#xff0c;任意两个不同的城市之间有且仅有一条直接或间接的道路。为…

在7分钟内深刻理解咖喱

Eric Elliott’s exceptional Composing Software series is initially what got me excited about functional programming. Its a must-read. 埃里克埃利奥特(Eric Elliott)杰出的合成软件系列最初使我对函数式编程感到兴奋。 这是必读的。 At one point in the series, he …

github后端开发面试题大集合(一)

作者&#xff1a;小海胆链接&#xff1a;https://www.nowcoder.com/discuss/3614?type0&order0&pos5&page0?fromwb来源&#xff1a;牛客网 转载自github&#xff0c;中文--->链接在这&#xff0c;英文---->链接在这文章较长&#xff0c;我把它拆分了三个部…

微信小程序左滑删除效果的实现完整源码附效果图

效果图&#xff1a; 功能描述&#xff0c;小程序列表左滑删除功能的实现完整源代码实现&#xff1a; <view wx:for{{friends}} wx:key"" wx:if{{groupType4}} catchtap"nav_oneChat" id"{{item._id}}" class"item p_r" style"…

Eclipse下修改工程名

一。 右键工程&#xff1a;Refactor->Rename&#xff0c;或选中工程按F2&#xff0c;修改名称 二。右键工程&#xff1a;Properties->Web Project Settings,修改Context Root三。1.找到项目所在位置&#xff08;如图&#xff09;&#xff1a; 2.修改项目目录/.setting目录…

git操作手册_基本的Git手册

git操作手册介绍 (Introduction) Hi! I am Sanjula, and in this guide I hope to teach you a little bit about Git including:嗨&#xff01; 我是Sanjula &#xff0c;在本指南中&#xff0c;我希望教您一些有关Git的知识&#xff0c;包括&#xff1a; What Git is 什么是…

PHP 接入(第三方登录)QQ 登录 OAuth2.0 过程中遇到的坑

前言 绝大多数网站都集成了第三方登录&#xff0c;降低了注册门槛&#xff0c;增强了用户体验。最近看了看 QQ 互联上 QQ 登录的接口文档。接入 QQ 登录的一般流程是这样的&#xff1a;先申请开发者 -> 然后创建应用&#xff08;拿到一组 AppId 和 AppKey&#xff09;-> …

npm全局环境变量配置,全局配置cnpm

今天新电脑想安装node.js &#xff0c; 发现最新版本的node.js已经不支持win7了&#xff0c;但是又不想换系统&#xff0c;所以找了个旧版本&#xff0c;这里不多说了。如果找不到旧版本的node下载&#xff0c;可以去我的QQ交流群文件里面下载&#xff0c;QQ群号&#xff1a;17…

NTFS for Mac OS X:使用Brew安裝NTFS-3G

在最新的OSX系統中&#xff0c;其實已經完整兼容NTFS文件系統&#xff0c;只是出於安全考慮默認是以只讀模式掛載NTFS分區的&#xff0c;可以透過diskutil查詢硬碟UUID&#xff0c;重新以讀寫(rw)權限掛載&#xff0c;具體的可以參照這裡 當然&#xff0c;也有現成的軟體幫助你…

javascript开关_JavaScript开关案例简介

javascript开关In this short article, I will introduce you to JavaScript switch cases and how to use them with practical examples.在这篇简短的文章中&#xff0c;我将向您介绍JavaScript切换案例以及如何通过实际示例使用它们。 This article will explain better wi…

C++中一个class类对象占用多少内字节(7个例子,很清楚)

一个空的class在内存中多少字节&#xff1f;如果加入一个成员函数后是多大&#xff1f;这个成员函数存储在内存中什么部分&#xff1f; 一个Class对象需要占用多大的内存空间。最权威的结论是&#xff1a; *非静态成员变量总合。 *加上编译器为了CPU计算&#xff0c;作出的数据…

Java学习----到底调用哪一个方法(多态)

public class Father {public void print() {System.out.println("Father:print()");} } public class Son extends Father{// 方法的覆盖&#xff1a;子类重写父类的同名方法 Overridepublic void print() {System.out.println("Son:print()");}// Father…

mpvue 转uni-app 操作记录

1.创建一个uni-app 的项目&#xff0c;把原有的mpvue项目手动迁移过来 2.用到mpvue特性的代码&#xff0c;需要修改成uni-app 兼容的&#xff0c;例如 mpvue-wxparse 可以修改成 3.src/app.json 改成 pages.json ,路径格式需要改&#xff0c;如下格式 {"pages": [&…

桌面应用程序 azure_Azure Logic应用程序用例–黑色星期五

桌面应用程序 azureThis blog gives an overview of how Azure Serverless technologies came to rescue when the custom-built integration system went down. Also, it shows the high-level architecture solution built using Azure Serverless services like Logic Apps,…

PHP的静态变量介绍

静态变量只存在于函数作用域内&#xff0c;也就是说&#xff0c;静态变量只存活在栈中。一般的函数内变量在函数结束后会释放&#xff0c;比如局部变量&#xff0c;但是静态变量却不会。就是说&#xff0c;下次再调用这个函数的时候&#xff0c;该变量的值会保留下来。 只要在变…

MySQL 解压版创建用户密码

root 权限进入MySQL&#xff1a; mysql –uroot 查看当前MySQL用户&#xff1a; select user,host from mysql.user; 此处以User为root&#xff0c;Host为localhost的账户为例&#xff0c;将密码改为password的命令&#xff1a; SET PASSWORD FOR rootlocalhost PASSWORD(newp…

git 忽略指定文件夹的上传

我们在使用 git 开发的时候&#xff0c;有些插件的模块文件通过npm install 就可以下载&#xff0c;一般是不上传到 git 中的&#xff08;因为文件太多会导致很耗时)&#xff0c;例如 我的 node_modules 文件夹&#xff0c;不想上传&#xff0c;我们应该这么做。 我们需要创建一…

数据库初学者_面向初学者的免费6小时数据科学课程

数据库初学者Data science is considered the "sexiest job of the 21st century." Learn data science in this full 6-hour course for absolute beginners from Barton Poulson of datalab.cc.数据科学被认为是“ 21世纪最艰巨的工作”。 通过datalab.cc的 Barton…

网页抓取及下载

downAndroidApk.php <?php /* 命令行 d: cd ApacheServer\php php.exe D:\ApacheServer\web\crawl\downAndroidApk.php --appidFileD:\ApacheServer\web\crawl\youxi.txt --newDirD:\ApacheServer\web\crawl\requestNewDir*/ // 判断必须在php-cli模式下运行&#xff0c;即…

javascript中关于this指向问题详解

前 言 LiuDaP 在前端的学习中&#xff0c;我们必然要用到js&#xff0c;js可以说是前端必不可少的的东西。在学习js的过程中&#xff0c;我们会经常用到this这个东西&#xff0c;而this的指向问题就变得尤为重要。今天正好有空闲时间&#xff0c;就给大家详细介绍一下js中关于…

mpvue 转uniapp 导航栏样式错乱问题修复 tabbar 样式修复

效果图&#xff1a;修改前&#xff0c;修改后 找了半天没找到原因&#xff0c;只能自己改样式了&#xff0c;下面是样式代码&#xff08;在app.vue 里面加上就行&#xff09; <style>/*每个页面公共css */uni-tabbar {box-sizing: border-box;position: fixed;left: 0;bo…

css规则_CSS规则,将使您的生活更轻松

css规则by Nick Gard尼克加德(Nick Gard) CSS规则&#xff0c;将使您的生活更轻松 (CSS rules that will make your life easier) After years of writing and maintaining a couple of very large web projects and numerous smaller ones, I have developed some heuristics…

在mybatis中模糊查询有三种写法

<select id"selectStudentsByName" resultType"Student"> <!--第一种--> <!-- select id,name,age,score from student where name like % #{0} % --> <!--第二种--> <!-- select id,name,age,score from student wher…

BZOJ 3566: [SHOI2014]概率充电器

题目&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id3566 首先这题正着想不好想&#xff0c;考虑补集转化。 先dfs一遍&#xff0c;令f[u](1-p[u])*∏(1-(1-f[v])*w) f[u]表示u这个点通过其子树并不能联通的概率。 然后考虑v从其父亲连过来的情况&#xff0c;设…

小程序云开发,订阅消息定时批量发送实现代码

需求&#xff1a;做一个类似抽奖结果通知的订阅消息提醒 实现流程&#xff1a; 每个用户需要先授权订阅消息接收&#xff0c;授权成功后把数据存到云开发的数据集合里面&#xff0c;再写个定时器&#xff0c;遍历数据集合的所有数据&#xff0c;拿到后遍历发送订阅消息&#…

机器学习速成课程

Learn the basics of machine learning and data science in this crash course tutorial for beginners from AI Sciences Academy. This course will give you the foundation you need to start learning more advanced material.在此速成课程教程中为AI Sciences Academy的…