Java 可编程外围设备数据接收
这听起来很基本,但我是Android BLE开发的初学者。 到目前为止,我能够创建我的Nexus 9设备作为外围设备,Moto G作为中央设备。另外,我正在成功连接设备。但我不知道,当我从中央设备发送一个特征时,它将从外设接收到哪里?仅当广告成功启动或未启动时,广告回调才会返回(在我的情况下,这是成功的) 这是我的外设代码Java 可编程外围设备数据接收,java,android,bluetooth,light,Java,Android,Bluetooth,Light,这听起来很基本,但我是Android BLE开发的初学者。 到目前为止,我能够创建我的Nexus 9设备作为外围设备,Moto G作为中央设备。另外,我正在成功连接设备。但我不知道,当我从中央设备发送一个特征时,它将从外设接收到哪里?仅当广告成功启动或未启动时,广告回调才会返回(在我的情况下,这是成功的) 这是我的外设代码 btleGattCallback = new BluetoothGattCallback() { @Override
btleGattCallback = new BluetoothGattCallback() {
@Override
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
// this will get called when a device connects or disconnects
}
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w("doscover", "onServicesDiscovered received: " + status);
}
}
};
BeaconParser bp = new BeaconParser().setBeaconLayout("m:2-3=0,i:4-19,p:20-20");
mBeaconTransmitter = new BeaconTransmitter(this, bp );
// Transmit a beacon with Identifiers 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 2
Long li = new Long(-0l);
ArrayList<Long> l = new ArrayList<Long>();
l.add(li);
Beacon beacon = new Beacon.Builder()
.setId1("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")
.setId2("1")
.setId3("2")
.setManufacturer(0x00ff) // Choose a number of 0x00ff or less as some devices cannot detect beacons with a manufacturer code > 0x00ff
.setTxPower(-59)
.setDataFields(l)
.build();
mBeaconTransmitter.startAdvertising(beacon);
btlegatcallback=new BluetoothGattCallback(){
@凌驾
//特征读取操作的结果
公共性质无效阅读(蓝牙gatt,
蓝牙特征,
int状态){
如果(状态==蓝牙GATT.GATT\U成功){
广播更新(动作数据可用,特征);
}
}
@凌驾
连接状态更改的公共无效(最终蓝牙gatt gatt、最终int状态、最终int新闻状态){
//当设备连接或断开连接时,将调用此函数
}
@凌驾
//新发现的服务
发现服务上的公共无效(Bluetooth gatt,int状态){
如果(状态==蓝牙GATT.GATT\U成功){
广播更新(发现行动、关税及贸易总协定服务);
}否则{
Log.w(“doscover”,“OnServicesDiscovery已接收:+状态”);
}
}
};
BeaconParser bp=new BeaconParser().setBeaconLayout(“m:2-3=0,i:4-19,p:20-20”);
MBeAcontransmiter=新的BeaconTransmiter(此,bp);
//发送带有标识符2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 2的信标
Long li=新长(-0升);
ArrayList l=新的ArrayList();
l、 加(li);
Beacon Beacon=新建Beacon.Builder()
.setId1(“2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6”)
.setId2(“1”)
.setId3(“2”)
.setManufacturer(0x00ff)//选择一个0x00ff或更少的数量,因为某些设备无法检测到制造商代码大于0x00ff的信标
.setTxPower(-59)
.setDataFields(l)
.build();
MBE发射机。开始转换(信标);
和中央密码
btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE) ;
madapter = btManager.getAdapter();
if (madapter != null && !madapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent,1);
}
mHandler = new Handler();
btleGattCallback = new BluetoothGattCallback() {
@Override
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
Log.w("doscover", "Connected " + status);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.w("discover", "onServicesDiscovered received: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w("doscover", "onServicesDiscovered received: " + status);
}
}
};
devicefound = new ArrayList<BluetoothDevice>();
devices = new ArrayAdapter<String>( this , R.layout.device_name);
ListView pairedListView = (ListView) findViewById(R.id.textView3);
pairedListView.setAdapter(devices);
mleScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
devicefound.add(device);
//UUID = device.getAddress().toString();
//Log.e("Search", "" + device.getName().toString() );
//Toast`.makeText(context,device.getName().toString(), 1 ).show();
Log.e("Search", "" + device.toString());
Log.e("Search", "" + String.valueOf(rssi) );
devices.add("Nex" + device.getName() );
//mBluetoothGatt = device.connectGatt(getActivity(), true, btleGattCallback);
}
};
pairedListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1,
int position, long id) {
dv = devicefound.get(position);
mBluetoothGatt = dv.connectGatt(getApplicationContext() , true, btleGattCallback);
mBluetoothGatt.connect();
dv.createBond();
//BluetoothDevice dd = madapter.getRemoteDevice(devicefound.get(position).getAddress());
if(i == false)
{
Toast.makeText(getApplicationContext(), "Service found. Connected to " + dv.getName() , 1).show();
}
else
{
Toast.makeText(getApplicationContext(), "Already Connected " + dv.getName() , 1).show();
}
i = mBluetoothGatt.connect();
mBluetoothGatt.beginReliableWrite();
boolean b = mBluetoothGatt.discoverServices();
Log.e("Discovery Started", "" + b );
Log.e("get ItemATPosition", "" + adapter.getItemAtPosition(position));
//BluetoothGattService Service = mBluetoothGatt.getService( );
// = Service.getCharacteristic(k);
BluetoothGattCharacteristic characteristic= new BluetoothGattCharacteristic(k,2,1); //Service
characteristic.setValue( "This is a Charects ");
byte[] value = {(byte)91,(byte)92,(byte)93};
characteristic.setValue(value);
boolean st = mBluetoothGatt.writeCharacteristic(characteristic);
//Toast.makeText(getActivity(), t1.getText() + " " + st, 1).show();
boolean b1 = mBluetoothGatt.executeReliableWrite();
//Toast.makeText(getActivity(), t1.getText() + " " + b1, 1).show();
Intent in = new Intent("ACTION_BOND_STATE_CHANGED.");
sendBroadcast(in);
}
});
scanLeDevice(true);
btManager=(蓝牙管理器)getSystemService(Context.BLUETOOTH\u服务);
madapter=btManager.getAdapter();
if(madapter!=null&&!madapter.isEnabled()){
Intent enableentent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用意图,1);
}
mHandler=新处理程序();
btlegatcallback=新的BluetoothGattCallback(){
@凌驾
//特征读取操作的结果
公共性质无效阅读(蓝牙gatt,
蓝牙特征,
int状态){
如果(状态==蓝牙GATT.GATT\U成功){
广播更新(动作数据可用,特征);
}
}
@凌驾
连接状态更改的公共无效(最终蓝牙gatt gatt、最终int状态、最终int新闻状态){
Log.w(“doscover”、“已连接”+状态);
}
@凌驾
发现服务上的公共无效(Bluetooth gatt,int状态){
Log.w(“发现”,“OnServicesDiscovery已接收:+状态”);
如果(状态==蓝牙GATT.GATT\U成功){
广播更新(发现行动、关税及贸易总协定服务);
}否则{
Log.w(“doscover”,“OnServicesDiscovery已接收:+状态”);
}
}
};
devicefound=新的ArrayList();
设备=新阵列适配器(此,R.layout.device\u名称);
ListView pairedListView=(ListView)findViewById(R.id.textView3);
pairedListView.setAdapter(设备);
mleScanCallback=新的BluetoothAdapter.LeScanCallback(){
@凌驾
public void onLeScan(最终BluetoothDevice设备、最终int rssi、最终字节[]扫描记录){
DeviceFund.add(设备);
//UUID=device.getAddress().toString();
//Log.e(“搜索”,“设备+device.getName().toString());
//Toast`.makeText(上下文,device.getName().toString(),1.show();
Log.e(“搜索”,“设备+device.toString());
Log.e(“Search”,“String.valueOf(rssi));
添加(“Nex”+device.getName());
//mbluetothgatt=device.connectGatt(getActivity(),true,btlegatcallback);
}
};
setOnItemClickListener(新的OnItemClickListener()){
@凌驾
公共单击(适配器视图适配器,视图arg1,
内部位置,长id){
dv=设备查找获取(位置);
mBluetoothGatt=dv.connectGatt(getApplicationContext(),true,btlegatcallback);
mBluetoothGatt.connect();
dv.createBond();
//BluetoothDevice dd=madapter.getRemoteDevice(deviceFund.get(position.getAddress());
如果(i==false)
{
Toast.makeText(getApplicationContext(),“找到服务。连接到”+dv.getName(),1.show();
}
其他的
{
Toast.makeText(getApplicationContext(),“已连接”+dv.getName(),1.show();
}
i=mBluetoothGatt.connect();
mBluetoothGatt.beginReliableWrite();
布尔b=mBluetoothGatt.discoverServices();
Log.e(“发现