Android studio 真机测试能搜索到蓝牙设备,但连接蓝牙时闪退。Messages未报错

禾曦走廊 2017-11-13 04:38:10
弄一个连接蓝牙控制照明开关的APP,但是真机测试时能够搜索到蓝牙,但是点连接就闪退。代码如下
public class Main6Activity extends Activity implements View.OnClickListener {
protected static final UUID MY_UUID = UUID.randomUUID(); // UUID.randomUUID()随机获取UUID
BluetoothDevice mDevice;
boolean isConnected;
TextView open;
private OutputStream os;

//线程类的实例

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
Button open = (Button) findViewById(R.id.open);
open.setOnClickListener(this);
Button colse=(Button) findViewById(R.id.hand);
colse.setOnClickListener(this);
initData();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RESULT_CANCELED:
Toast.makeText(Main6Activity.this, "获取socket异常" + msg.obj, Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(Main6Activity.this, "正在连接设备", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(Main6Activity.this, "连接成功", Toast.LENGTH_SHORT).show();
break;

default:
break;
}
};
};
private BluetoothSocket mBluetoothSocket;
private OutputStream mOutputStream;
public void initData() {
Intent intent = getIntent();
mDevice = intent.getParcelableExtra("device");
if (mDevice == null) {
Toast.makeText(this, "传递的数据不能为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {

@Override
public void run() {
try {
if (mBluetoothSocket == null) {
mBluetoothSocket = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
}
} catch (IOException e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = RESULT_CANCELED;
msg.obj = e;
handler.sendMessage(msg);
}

try {
if (!mBluetoothSocket.isConnected()) {
handler.sendEmptyMessage(1);
mBluetoothSocket.connect();
}
} catch (IOException e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = RESULT_CANCELED;
msg.obj = e;
handler.sendMessage(msg);
}

try {
if (mOutputStream == null) {
mOutputStream = mBluetoothSocket.getOutputStream();
}
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
Message msg = Message.obtain();
msg.what = RESULT_CANCELED;
msg.obj = e;
handler.sendMessage(msg);
}
isConnected = true;
handler.sendEmptyMessage(2);
}
}).start();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.open:
sendCommand(Const.OPEN);
break;
case R.id.colse:
sendCommand(Const.CLOSE);
break;
}
}
private void sendCommand(byte[] command) {
if (!isConnected) {
Toast.makeText(this, "设备还未连接", Toast.LENGTH_SHORT).show();
return;
}
try {
mOutputStream.write(command);
} catch (IOException e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = RESULT_CANCELED;
msg.obj = e;
handler.sendMessage(msg);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
try {
if (mOutputStream != null) {
mOutputStream.close();
mOutputStream = null;
}
if (mBluetoothSocket!=null&&mBluetoothSocket.isConnected()) {
mBluetoothSocket.close();
mBluetoothSocket = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

.xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/context2"
tools:context="com.example.administrator.zhy.Main6Activity">

<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="off"
android:textSize="30dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"/>
<Button
android:id="@+id/colse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:layout_marginTop="100dp"
android:text="no"
android:textSize="30dp"/>

</RelativeLayout>

刚学as,代码大多数都是百度照着打的,求大神帮忙看看那里错了
...全文
400 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
禾曦走廊 2017-11-14
  • 打赏
  • 举报
回复
9:53 Executing tasks: [:app:assembleDebug] 9:54 Gradle build finished in 17s 379ms 9:54 Can't bind to local 8600 for debugger
禾曦走廊 2017-11-14
  • 打赏
  • 举报
回复
我不知道怎么导出错误log,大神能加我qq远程看看吗? qq:453550412(膜拜)
ghostgoodbye 2017-11-14
  • 打赏
  • 举报
回复
提供一下报错log

80,351

社区成员

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

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