YDBluetoothWebBridge 0.2.5

YDBluetoothWebBridge 0.2.5

许可证 MIT
发布最新发布2017年9月

felix 维护。



 
依赖
Masonry~> 1.0.2
BabyBluetooth~> 0.7.0
WebViewJavascriptBridge~> 6.0.2
SVProgressHUD~> 2.1.2
NJKWebViewProgress~> 0.2.3
YYModel~> 1.0.4
MJRefresh~> 3.1.12
Reachability~> 3.2
HardwareDatasStorage~> 0.0.1
 

  • 作者
  • felix

这是一个用于开发类似耳机和显示屏等蓝牙连接硬件的 HTML 框架

此框架依赖于一些第三方代码,如下

   s.dependency 'Masonry', '~> 1.0.2'
    s.dependency 'BabyBluetooth', '~> 0.7.0'
    s.dependency 'WebViewJavascriptBridge', '~> 6.0.2'
    s.dependency 'SVProgressHUD', '~> 2.1.2'
    s.dependency 'NJKWebViewProgress', '~> 0.2.3'
    s.dependency 'YYModel', '~> 1.0.4'
    s.dependency 'MJRefresh', '~> 3.1.12'
    s.dependency 'Reachability', '~> 3.2'
    s.dependency 'HardwareDatasStorage', '~> 0.0.1'

YDOpenHardware(HardwareDatasStorage)

用于悦动圈跑步开放平台的简单 demo 问题咨询: qq:295235985 邮箱:[email protected]

第三方最终提供给我们一个 framework,模拟器和真机通用的 YDOpenHardwarePod

存储蓝牙数据,您可以从存储选择由 sqlite(基于 fmdb 框架)存储的数据;如:心率 / 步数计 / 智能体重秤 / 睡眠等,这些我们需要扩展。您可以通过 js 程序语言从 html 中调用这些方法

function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}

2、 调用最后一个方法进行必要的业务操作

setupWebViewJavascriptBridge(function(bridge) {
…………………………
to do the business necessary
}

3、 sj 调用 oc 方法

var callbackButton = document.getElementById('scanBtn')
callbackButton.innerHTML = 'scanBtn'
callbackButton.onclick = function(e) {
e.preventDefault();
log('JS calling handler "scan peripheral"')
// window.location.href='./peripheralList.html';
bridge.callHandler('onScanS3Click', 'S3', function(response) {
log('JS got response', response)
});
};

4、 oc 调用 js 方法,因此我们必须注册这些方法

bridge.registerHandler('deliverCharacteristic', function(data, responseCallback) {
log('data'+data+':'+data.uuid+data.value.value0 +':'+data.value.value1+':'+data.value.value2+':'+data.value.value3+'.');

var stepLabel = document.getElementById('stepLabel');
// stepLabel.innerHTML =
var calorieLabel = document.getElementById('calorieLabel');
// calorieLabel.innerHTMLp =
var distanceLabel = document.getElementById('distanceLabel');
// distanceLabel.innerHTML =

responseCallback(responseData);
});

5、 数据传递协议:我们必须通过 json 格式传递数据,您可以传递 json 对象或数据字符串等,这些可以传递给 oc;因此我们推荐格式为 key:value,它更简单,更容易使用。例如,oc 中的数据:

@{
@"uuid":c.UUID.UUIDString,
@"value":@{
@"value0":value0,
@"value1":value1,
@"value2":value2,
@"value3":value3
}
};

更详细的内容,您可以在 S3.html 中看到

下面是存储和缓存数据的 js 的调用方法

体称
insertIntelligentScale
selectNewIntelligentScaleByInfo
selectIntelligentScaleByInfo
selectIntelligentScaleInPageByInfo

心率
insertHeartRate
selectNewHeartRateByInfo
selectHeartRateByInfo
selectHeartRateInPageByInfo

步数
insertPedometer
selectNewPedometerByInfo
selectPedometerByInfo
selectPedometerInPageByInfo

睡眠
insertSleep
selectNewSleepByInfo
selectSleepByInfo
selectSleepInPageByInfo

参数的传入:parmas
格式:dictionary || (key/value) || jsonObject

以下是数据模型的关键代码

@interface YDOpenHardwareSleep : NSObject

@property (nonatomic, strong) NSNumber *ohsId;//对应数据库字段:ohs_id
@property (nonatomic, strong) NSString *deviceId;//对应数据库字段:device_id
@property (nonatomic, strong) NSNumber *sleepSec;//对应数据库字段:sleep_sec
@property (nonatomic, strong) NSNumber *sleepSection;//对应数据库字段:sleep_section
@property (nonatomic, strong) NSDate *startTime;//对应数据库字段:start_time
@property (nonatomic, strong) NSDate *endTime;//对应数据库字段:end_time
@property (nonatomic, strong) NSNumber *userId;//对应数据库字段:user_id
@property (nonatomic, strong) NSString *extra;//对应数据库字段:extra
@property (nonatomic, strong) NSNumber *serverId;//对应数据库字段:server_id
@property (nonatomic, strong) NSNumber *status;//对应数据库字段:status
@end

@interface YDOpenHardwareUser : NSObject
@property (nonatomic, copy) NSString *province; //所在省
@property (nonatomic, copy) NSString *city; //城市
@property (nonatomic, strong) NSNumber *userID; //用户id
@property (nonatomic, strong) NSNumber *rank; //用户等级
@property (nonatomic, strong) NSNumber *sex; //0为男,1为女
@property (nonatomic, copy) NSString *nick; //用户昵称
@property (nonatomic, copy) NSString *headImageUrl; //头像url
@property (nonatomic, copy) NSString *loveSports; //用户喜欢的运动 逗号隔开
@property (nonatomic, copy) NSString *phone; //用户手机
@property (nonatomic, copy) NSString *signature; //用户签名
@property (nonatomic, strong) NSDate *birth; //生日
@property (nonatomic, strong) NSNumber *height; //身高cm
@property (nonatomic, strong) NSNumber *weight; //重量g
@end


@interface YDOpenHardwarePedometer : NSObject
@property (nonatomic, strong) NSNumber *ohpId;//对应数据库字段:ohp_id
@property (nonatomic, strong) NSString *deviceId;//对应数据库字段:device_id
@property (nonatomic, strong) NSNumber *numberOfStep;//对应数据库字段:number_of_step
@property (nonatomic, strong) NSNumber *distance;//对应数据库字段:distance
@property (nonatomic, strong) NSNumber *calorie;//对应数据库字段:calorie
@property (nonatomic, strong) NSDate *startTime;//对应数据库字段:start_time
@property (nonatomic, strong) NSDate *endTime;//对应数据库字段:end_time
@property (nonatomic, strong) NSNumber *userId;//对应数据库字段:user_id
@property (nonatomic, strong) NSString *extra;//对应数据库字段:extra
@property (nonatomic, strong) NSNumber *serverId;//对应数据库字段:server_id
@property (nonatomic, strong) NSNumber *status;//对应数据库字段:status
@end


@interface YDOpenHardwareIntelligentScale : NSObject
@property (nonatomic, strong) NSNumber *ohiId;//对应数据库字段:ohi_id
@property (nonatomic, strong) NSString *deviceId;//对应数据库字段:device_id
@property (nonatomic, strong) NSDate *timeSec;//对应数据库字段:time_sec
@property (nonatomic, strong) NSNumber *weightG;//对应数据库字段:weight_g
@property (nonatomic, strong) NSNumber *heightCm;//对应数据库字段:height_cm
@property (nonatomic, strong) NSNumber *bodyFatPer;//对应数据库字段:body_fat_per
@property (nonatomic, strong) NSNumber *bodyMusclePer;//对应数据库字段:body_muscle_per
@property (nonatomic, strong) NSNumber *bodyMassIndex;//对应数据库字段:body_mass_index
@property (nonatomic, strong) NSNumber *basalMetabolismRate;//对应数据库字段:basal_metabolism_rate
@property (nonatomic, strong) NSNumber *bodyWaterPercentage;//对应数据库字段:body_water_percentage
@property (nonatomic, strong) NSNumber *userId;//对应数据库字段:user_id
@property (nonatomic, strong) NSString *extra;//对应数据库字段:extra
@property (nonatomic, strong) NSNumber *serverId;//对应数据库字段:server_id
@property (nonatomic, strong) NSNumber *status;//对应数据库字段:status
@end


@interface YDOpenHardwareHeartRate : NSObject
@property (nonatomic, strong) NSNumber *ohhId;//对应数据库字段:ohh_id
@property (nonatomic, strong) NSString *deviceId;//对应数据库字段:device_id
@property (nonatomic, strong) NSNumber *heartRate;//对应数据库字段:heart_rate
@property (nonatomic, strong) NSDate *startTime;//对应数据库字段:start_time
@property (nonatomic, strong) NSDate *endTime;//对应数据库字段:end_time
@property (nonatomic, strong) NSNumber *userId;//对应数据库字段:user_id
@property (nonatomic, strong) NSString *extra;//对应数据库字段:extra
@property (nonatomic, strong) NSNumber *serverId;//对应数据库字段:server_id
@property (nonatomic, strong) NSNumber *status;//对应数据库字段:status
@end

注意:device_id & user_id 默认不用传入,在native 这边默认是当前的用户设备
其他字段按需传入

` #蓝牙操作过程中的几个必要的方法

var callbackButton = document.getElementById('scanBtn')
callbackButton.innerHTML = 'scanBtn'
callbackButton.onclick = function(e) {
e.preventDefault();
log('JS calling handler "scan peripheral"')
// window.location.href='./peripheralList.html';
var scanParams = {'prefixField':'S3','YDBlueToothFilterType':3};
bridge.callHandler('onScanClick', scanParams, function(response) {
log('JS got response', response)
});
};
method: onScanClick
params: scanParams (key/value)

第一个参数是对应的过滤的类型:

YDBlueToothFilterTypeNone = 0,
YDBlueToothFilterTypeMatch,  // match to filter and find the specify device
YDBlueToothFilterTypeContain,    // contain the keyword to filter and find the specify device
YDBlueToothFilterTypePrefix,     // key word by the prefix
YDBlueToothFilterTypeSuffix,     // key word by the suffix
YDBlueToothFilterTypePrefixAndSuffix, // key word by the prefix & suffix
YDBlueToothFilterTypePrefixAndContain, // key word by the prefix & contain
YDBlueToothFilterTypeSuffixAndContrain, // key word by the suffix & contain
YDBlueToothFilterTypePrefixAndContrainAndSuffix, //key word by the prefix & contrain * suffix

第二个参数是对应的过滤方式的字段:

@property (nonatomic, copy) NSString *matchField; // 匹配
@property (nonatomic, copy) NSString *prefixField; // 前缀
@property (nonatomic, copy) NSString *suffixField; // 后缀
@property (nonatomic, copy) NSString *containField; //包含

写数据

format : var datas = {"hexString":"0x06","length":1};

writeDatas.onclick = function(e) {
e.preventDefault();
// test value
var datas = {"hexString":"0x06","length":1};
bridge.callHandler('writeDatas',datas,function(response) {
log('respone +',response);
});
};

……