BLE写数据问题,还麻烦大神帮看一下

xqq12345 2015-07-28 03:43:27
要根据蓝牙ble demo,修改出一个简单的蓝牙测试APP,实现写数据发指令,遇到个问题,请高手帮我解答下:

写数据方法如下

public void writeCharacteristic(String value,UUID uuid1,UUID uuid2) {
System.out.println(value+"/n"+uuid1.toString()+"/n"+uuid2.toString());
BluetoothGattService LumService = mBluetoothGatt
.getService(uuid1);

if (LumService == null) {
System.out.println("LumService null");
return;
}


BluetoothGattCharacteristic LumChar = LumService.getCharacteristic(uuid2);
if (LumChar == null) {
System.out.println("LumChar null");
return;
}
LumChar.setValue(value);
boolean status = mBluetoothGatt.writeCharacteristic(LumChar);
System.out.println("Write Status: " + status);
}


但是在调用的时候如果连接成功后写入
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_connect:
final EditText passwordEdit = new EditText(DeviceControlActivity.this);
AlertDialog.Builder builder = new Builder(DeviceControlActivity.this);
builder.setTitle("请输入");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setView( passwordEdit);
builder.setPositiveButton("确定" , new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Passwd = passwordEdit.getText().toString();
Toast.makeText(getApplicationContext(), Passwd, Toast.LENGTH_LONG).show();
mBluetoothLeService.connect(mDeviceAddress);

mBluetoothLeService.writeCharacteristic(Passwd, uuid, uuid);

就会打印LumChar null,
但是在
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
updateConnectionState(R.string.connected);
invalidateOptionsMenu();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
displayGattServices(mBluetoothLeService.getSupportedGattServices());
mBluetoothLeService.writeCharacteristic(Passwd, uuid, uuid2);
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
}
}
};


就能成功写入数据,还麻烦高手们指点一下,后续还要写数据发指令,我该怎么调用写数据的方法

...全文
717 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_34004358 2018-12-03
  • 打赏
  • 举报
回复
mBluetoothLeService.connect(mDeviceAddress); 有返回的对象,判断一点是否为空。不为空才连接成功
一光年lost 2016-07-26
  • 打赏
  • 举报
回复
时隔一年代码还在不 - - 在的话能给我发一份吗,新手非常需要,374863627@qq.com
liufaqst 2016-02-14
  • 打赏
  • 举报
回复
9楼正解!connect是个异步的方法,需要时间的。
vsjkai 2015-09-24
  • 打赏
  • 举报
回复
错误的地方是mBluetoothLeService.connect(mDeviceAddress);后你直接写数据,这个时候不一定连接上了,因为connect不是你调用了马上就连接的。成功的地方是已经发现服务表明已经连接上了,这个时候再写数据就能够成功。理论上只要连接成功后,后续都能写数据
jarvan04 2015-08-25
  • 打赏
  • 举报
回复
楼主,问题[align=right]解决了么,能给一份代码不?谢谢 Q:2373235560@qq.com
baililanxue 2015-08-24
  • 打赏
  • 举报
回复
楼主 能发下写数据的代码吗 邮箱 wq952845839@163.com 谢谢了
—— 2015-08-21
  • 打赏
  • 举报
回复
楼主能给我发一分代码吗,正好在做ble,邮箱2196285091@qq.com
sk_lin 2015-07-29
  • 打赏
  • 举报
回复
呵呵,这用的别人的框架的吧?
xqq12345 2015-07-29
  • 打赏
  • 举报
回复
好伤心啊,大神们,求指点啊
xqq12345 2015-07-29
  • 打赏
  • 举报
回复
还请高手指点一下啊
xqq12345 2015-07-29
  • 打赏
  • 举报
回复
引用 3 楼 u014116982 的回复:
呵呵,这用的别人的框架的吧?
对啊,添加个写入的方法支持写几条指令的测试一下就行了

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧