Skip to content

Commit

Permalink
QVQ
Browse files Browse the repository at this point in the history
  • Loading branch information
pencilso committed Dec 15, 2017
1 parent 61bb18a commit 502f2b8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ManyBlue/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.1.7"
versionName "1.1.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
14 changes: 14 additions & 0 deletions ManyBlue/src/main/java/io/javac/ManyBlue/ManyBlue.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;
Expand Down Expand Up @@ -439,4 +440,17 @@ public static <T extends Instructions> T getDeviceInstructions(Object tag) {
if (tag == null) throw new NullPointerException("tag can'not null");
return (T) BluetoothGattManager.getGatt(tag).getInstructions();
}

/**
* 设置写入数据到蓝牙的方式
* BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT | Write characteristic, requesting acknoledgement by the remote device
* BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE | Wrtite characteristic without requiring a response by the remote device
* BluetoothGattCharacteristic.WRITE_TYPE_SIGNED | Write characteristic including authentication signature
*
* @param type
* @param tag
*/
public static void setBlueWriteType(int type, Object tag) {
BluetoothGattManager.getGatt(tag).setWrite_type(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class BlueGattCallBack extends BluetoothGattCallback {
private BluetoothGattCharacteristic characteristic_read;//读取通道
private Instructions instructions;//指令对象


private int write_type = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;//写入数据类型

public Object getTag() {
return tag;
}
Expand Down Expand Up @@ -125,13 +128,13 @@ public void registerDevice(UUIDMessage uuidMessage) {
}

public void write_data(String data) {
characteristic_write.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
characteristic_write.setWriteType(write_type);
characteristic_write.setValue(data);
boolean writeCharacteristic = bluetoothGatt.writeCharacteristic(characteristic_write);
}

public void write_data(byte by[]) {
characteristic_write.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
characteristic_read.setWriteType(write_type);
characteristic_write.setValue(by);
boolean writeCharacteristic = bluetoothGatt.writeCharacteristic(characteristic_write);
}
Expand Down Expand Up @@ -241,4 +244,8 @@ public void setInstructions(Instructions instructions) {
public Instructions getInstructions() {
return instructions;
}

public void setWrite_type(int write_type) {
this.write_type = write_type;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Github仓库地址 [https://github.com/pencilso/ManyBlue](https://github.com/pen

dependencies {
compile 'com.github.pencilso:ManyBlue:v1.1.7'
compile 'com.github.pencilso:ManyBlue:v1.1.8'
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.javac.manybluesample.ui.main;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;

import org.greenrobot.eventbus.Subscribe;
Expand Down Expand Up @@ -82,4 +86,19 @@ public void onServiceStart() {
public void onServiceStop() {
appToast("蓝牙服务已关闭");
}

@Override
protected void onStart() {
super.onStart();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, 0);
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
//判断是否跟用户做一个说明
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.javac.manybluesample.ui.scanner;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -63,6 +64,7 @@ public void onDeviceScanner(BluetoothDevice device) {

/**
* 扫描到的蓝牙设备列表
*
* @param device
*/
@Override
Expand Down Expand Up @@ -90,10 +92,16 @@ public void onDeviceServiceDiscover(List<BluetoothGattService> services, Object
// services.get(0).getUuid().toString();//这是获取UUID的方法
//找到需要的UUID服务 然后进行连接 比如说我需要的服务UUID是00003f00-0000-1000-8000-00805f9b34fb UUID的话 一般设备厂家会提供文档 都有写的
UUIDMessage uuidMessage = new UUIDMessage();//创建UUID的配置类
uuidMessage.setCharac_uuid_service("00003f00-0000-1000-8000-00805f9b34fb");//需要注册的服务UUID
uuidMessage.setCharac_uuid_write("00003f02-0000-1000-8000-00805f9b34fb");//写出数据的通道UUID
uuidMessage.setCharac_uuid_read("00003f01-0000-1000-8000-00805f9b34fb");//读取通道的UUID
uuidMessage.setDescriptor_uuid_notify("00002902-0000-1000-8000-00805f9b34fb");//这是读取通道当中的notify通知
// uuidMessage.setCharac_uuid_service("00003f00-0000-1000-8000-00805f9b34fb");//需要注册的服务UUID
// uuidMessage.setCharac_uuid_write("00003f02-0000-1000-8000-00805f9b34fb");//写出数据的通道UUID
// uuidMessage.setCharac_uuid_read("00003f01-0000-1000-8000-00805f9b34fb");//读取通道的UUID
// uuidMessage.setDescriptor_uuid_notify("00002902-0000-1000-8000-00805f9b34fb");//这是读取通道当中的notify通知


uuidMessage.setCharac_uuid_service("0000b0f0-0000-1000-8000-00805f9b34fb");
uuidMessage.setCharac_uuid_write("0000b0f7-0000-1000-8000-00805f9b34fb");
uuidMessage.setCharac_uuid_read("0000b0f6-0000-1000-8000-00805f9b34fb");
uuidMessage.setDescriptor_uuid_notify("00002902-0000-1000-8000-00805f9b34fb");
/**
* 这里简单说一下 如果设备返回数据的方式不是Notify的话 那就意味着向设备写出数据之后 再自己去获取数据
* Notify的话 是如果蓝牙设备有数据传递过来 能接受到通知
Expand All @@ -106,6 +114,7 @@ public void onDeviceServiceDiscover(List<BluetoothGattService> services, Object
public void onDeviceRegister(boolean state, Object tag) {
dismissDialog();
appToast(state ? "设备注册成功" : "设备注册失败");
if (state)ManyBlue.setBlueWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE,tag);
}


Expand Down

0 comments on commit 502f2b8

Please sign in to comment.