我使用的是蓝牙实现串口传输SPP发送和接受数据的时候,在程序退出后,整个手机就卡死 。程序代码如下:public class BluetoothManager {
public static final int cnadata = 0x7D;
public static final int cnbdata = 0x80;
public static final int cncdata = 0xAF;
public static final int cnddata = 0xA1;
public static final int cnedata = 0x81;
static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";//UUID
public static InputStream mmInStream;
public static OutputStream mmOutStream;
public List<String> lstDevices = new ArrayList<String>();
public BluetoothAdapter mbtAdapt;
public BluetoothSocket mbtSocket;
public boolean mBTConnect;
public SpData mSp;
Activity mDlg;
public BluetoothManager(Context context)
{
mDlg = (Activity) context;
init();
}
public void init()
{
//mDlg = (Activity) context;
mSp = new SpData();
mbtAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
mDlg.registerReceiver(searchDevices, intent); }
private BroadcastReceiver searchDevices = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
Object[] lstName = b.keySet().toArray(); for (int i = 0; i < lstName.length; i++) {
String keyName = lstName[i].toString();
Log.d("Name",keyName);
}
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String strName = device.getName();
String str = device.getName() + "|" + device.getAddress();
if (lstDevices.indexOf(str) == -1)// 防止重复添加
{
lstDevices.add(str); // 获取设备名称和mac地址
}
}
}
};
public void stop()
{
if (null == mbtAdapt)
return;
if (!mbtAdapt.isEnabled())
return;
if (mbtAdapt.isDiscovering())
mbtAdapt.cancelDiscovery();
if(searchDevices != null)
mDlg.unregisterReceiver(searchDevices);
try {
if(mmOutStream != null)
{
mmOutStream.close();
mmOutStream = null;
}
if(mmInStream != null)
{
mmInStream.close();
mmInStream = null;
}
if(mbtSocket != null)
{
mbtSocket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void strat()
{
SendData();
}
private void SendData()
{
try {
byte[] data = new byte[9];
data[0] = cnadata;
data[1] = (byte) cnedata;
data[2] = (byte) cnddata;
data[3] = (byte) cnbdata;
data[4] = (byte) cnbdata;
data[5] = (byte) cnbdata;
data[6] = (byte) cnbdata;
data[7] = (byte) cnbdata;
data[8] = (byte) cnbdata; mmOutStream.write(data); readData rd=new readData();
rd.start();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("exception");
e.printStackTrace();
}
}
public void connectBluetooth(String address)
{
mbtAdapt.cancelDiscovery();
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothDevice btDev = mbtAdapt.getRemoteDevice(address);
try {
mbtSocket = btDev.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mbtSocket.connect();
mBTConnect = true;
mmInStream = mbtSocket.getInputStream();
mmOutStream= mbtSocket.getOutputStream();
}
catch (IOException e) {
//TODO Auto-generated catch block
mBTConnect = false;
try {
mbtSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mbtSocket = null;
e.printStackTrace();
}
}
public void SearchBluetooth()
{
mbtAdapt.startDiscovery();
}
class readData extends Thread{ @Override
public void run() {
// TODO Auto-generated method stub
System.out.println("run");
InputStream in = null;
try {
in = mbtSocket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final int len = 9;
byte b[] = new byte[len];
if (in!= null)
{
System.out.println("input not null!");
try {
while (in.read(b) != -1) {
mSp.setData(b,len);
mSp.SubEndSaveing();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("read wrong");
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}我在activity退出的时候调用了stop(),但是程序退出后,手机就卡死了·点击什么不能动了,有没有知道这个问题怎么解决
public static final int cnadata = 0x7D;
public static final int cnbdata = 0x80;
public static final int cncdata = 0xAF;
public static final int cnddata = 0xA1;
public static final int cnedata = 0x81;
static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";//UUID
public static InputStream mmInStream;
public static OutputStream mmOutStream;
public List<String> lstDevices = new ArrayList<String>();
public BluetoothAdapter mbtAdapt;
public BluetoothSocket mbtSocket;
public boolean mBTConnect;
public SpData mSp;
Activity mDlg;
public BluetoothManager(Context context)
{
mDlg = (Activity) context;
init();
}
public void init()
{
//mDlg = (Activity) context;
mSp = new SpData();
mbtAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
mDlg.registerReceiver(searchDevices, intent); }
private BroadcastReceiver searchDevices = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
Object[] lstName = b.keySet().toArray(); for (int i = 0; i < lstName.length; i++) {
String keyName = lstName[i].toString();
Log.d("Name",keyName);
}
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String strName = device.getName();
String str = device.getName() + "|" + device.getAddress();
if (lstDevices.indexOf(str) == -1)// 防止重复添加
{
lstDevices.add(str); // 获取设备名称和mac地址
}
}
}
};
public void stop()
{
if (null == mbtAdapt)
return;
if (!mbtAdapt.isEnabled())
return;
if (mbtAdapt.isDiscovering())
mbtAdapt.cancelDiscovery();
if(searchDevices != null)
mDlg.unregisterReceiver(searchDevices);
try {
if(mmOutStream != null)
{
mmOutStream.close();
mmOutStream = null;
}
if(mmInStream != null)
{
mmInStream.close();
mmInStream = null;
}
if(mbtSocket != null)
{
mbtSocket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void strat()
{
SendData();
}
private void SendData()
{
try {
byte[] data = new byte[9];
data[0] = cnadata;
data[1] = (byte) cnedata;
data[2] = (byte) cnddata;
data[3] = (byte) cnbdata;
data[4] = (byte) cnbdata;
data[5] = (byte) cnbdata;
data[6] = (byte) cnbdata;
data[7] = (byte) cnbdata;
data[8] = (byte) cnbdata; mmOutStream.write(data); readData rd=new readData();
rd.start();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("exception");
e.printStackTrace();
}
}
public void connectBluetooth(String address)
{
mbtAdapt.cancelDiscovery();
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothDevice btDev = mbtAdapt.getRemoteDevice(address);
try {
mbtSocket = btDev.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mbtSocket.connect();
mBTConnect = true;
mmInStream = mbtSocket.getInputStream();
mmOutStream= mbtSocket.getOutputStream();
}
catch (IOException e) {
//TODO Auto-generated catch block
mBTConnect = false;
try {
mbtSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mbtSocket = null;
e.printStackTrace();
}
}
public void SearchBluetooth()
{
mbtAdapt.startDiscovery();
}
class readData extends Thread{ @Override
public void run() {
// TODO Auto-generated method stub
System.out.println("run");
InputStream in = null;
try {
in = mbtSocket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final int len = 9;
byte b[] = new byte[len];
if (in!= null)
{
System.out.println("input not null!");
try {
while (in.read(b) != -1) {
mSp.setData(b,len);
mSp.SubEndSaveing();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("read wrong");
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}我在activity退出的时候调用了stop(),但是程序退出后,手机就卡死了·点击什么不能动了,有没有知道这个问题怎么解决
12-08 17:32:23.914: INFO/BTL-IFS-WRAPPER(404): check_data_sock: [DATA] Channel disconnected [brcm.bt.dtun]
12-08 17:32:23.914: DEBUG/BTL-IFS-WRAPPER(404): notify_data_ch_disc_ind: [notify_data_ch_disc_ind] notify_data_ch_disc_ind: brcm.bt.dtun sub 15 (22:-1)
12-08 17:32:23.914: INFO/BTL-IFS(404): btl_if_notify_rx_buf_pending: rx buffer pending, notify user (0x40217bea 127)
12-08 17:32:23.914: DEBUG/(404): btlif_bts_api_ctrl_cb: btlif_bts_api_ctrl_cb : id BTLIF_DISC_RX_BUF_PENDING (4115) on hdl -1
12-08 17:32:23.914: INFO/(404): btlif_rfc_rx_buf_pending: free rx buf 40217bea
12-08 17:32:23.914: DEBUG/(404): btlif_bts_api_ctrl_cb: btlif_bts_api_ctrl_cb : id BTLIF_DATA_CH_DISC_IND (4111) on hdl 20
12-08 17:32:23.914: INFO/(404): btlif_rfc_data_chan_disc_ind: disconnected hdl 22
12-08 17:32:23.914: INFO/BTL-IFS-WRAPPER(404): wrp_close_data: wrp_close 22 [brcm.bt.dtun]
12-08 17:32:23.914: DEBUG/BTL-IFS-WRAPPER(404): wsactive_del: delete wsock 22 from active list [1cb384]
12-08 17:32:23.924: DEBUG/(404): btlif_bts_api_ctrl_cb: btlif_bts_api_ctrl_cb : id BTLIF_BTS_RFC_CLOSE (4459) on hdl 20
12-08 17:32:23.924: DEBUG/(404): btlif_rfc_close: scn 1, sock 38
12-08 17:32:23.924: DEBUG/(404): btlif_rfc_close: type 0, dhdl -1, bta_hdl 2, scn 1, phdl 7, lstpnd 0, -1:38 (L:D)
12-08 17:32:23.924: INFO/(404): btlif_rfc_close: close rfcomm connection
12-08 17:32:23.924: DEBUG/(404): bts_chan_free: p = 0x201bb0
12-08 17:32:23.924: DEBUG/BTL-IFS-WRAPPER(404): wrp_free_dynamicport: free dyn port 9050 (s:0), sub 1
12-08 17:32:23.924: DEBUG/(404): bts_chan_free: bts_chan_free success, bta hdl 2 (tot 1)
12-08 17:32:23.924: DEBUG/(404): bts_chan_dump_all: ### channel list ###
12-08 17:32:23.924: DEBUG/(404): bts_chan_dump_all: type 0, dhdl -1, bta_hdl 1, scn 19, phdl 65535, lstpnd 0, 30:-1 (L:D)
12-08 17:32:23.934: INFO/System.out(724): read wrong
12-08 17:32:23.944: DEBUG/(404): jw_if_rfcomm_cl_cback: jw_if_rfcomm_cl_cback event=BTA_JV_RFCOMM_CLOSE_EVT
12-08 17:32:23.944: INFO/(404): jv_rfc_close: jv_rfc_close hdl 2, status 1, remotely initiated 0, result 19, port status 269483548
12-08 17:32:23.944: DEBUG/(404): jv_rfc_close: channel already disconnected
12-08 17:32:23.944: WARN/System.err(724): java.io.IOException: socket closed
12-08 17:32:23.964: DEBUG/Tethering(182): active iface (usb0) reported as added, ignoring
12-08 17:32:23.964: WARN/System.err(724): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:306)
12-08 17:32:23.964: WARN/System.err(724): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
12-08 17:32:23.974: WARN/NotificationService(182): --Intent.ACTION_BATTERY_CHANGED recived !!!
12-08 17:32:23.974: WARN/NotificationService(182): --hschoi ACTION_BATTERY_CHANGED recived plugType!!!2
12-08 17:32:23.974: INFO/NotificationService(182): --hschoi isUSBPlugged :: usb_status = USB_STATE_CONFIGURED
12-08 17:32:23.974: VERBOSE/NotificationService(182): [enqueueNotificationWithTag]::::: pkg:android, id:17040317, notification:天翼宽带
12-08 17:32:23.974: WARN/NotificationService(182): --hschoi updateUSBPluggedNotification !!!
12-08 17:32:23.984: WARN/System.err(724): at java.io.InputStream.read(InputStream.java:159)
12-08 17:32:23.984: INFO/StatusBarPolicy(182): updateBattery end
12-08 17:32:23.984: DEBUG/WifiService(182): [mPersistState] : On
12-08 17:32:23.984: DEBUG/NotificationService(182): isOffhook(): falsegetAlertOnCallMode0status:1
12-08 17:32:23.984: DEBUG/NotificationService(182): mSystemReady: true
12-08 17:32:23.984: DEBUG/NotificationService(182): enqueueNotification :: notification.defaults = 0, Notification.DEFAULT_SOUND = 1, notification.sound = null
12-08 17:32:23.984: INFO/StatusBarPolicy(182): updateBattery current_timeout = 30000 current_brightness = 141
12-08 17:32:23.984: INFO/StatusBarPolicy(182): BAT. status:2 health:2
12-08 17:32:23.984: DEBUG/WifiService(182): ACTION_BATTERY_CHANGED pluggedType: 2
12-08 17:32:23.994: WARN/System.err(724): at com.common.BluetoothDiscovery$readData.run(BluetoothDiscovery.java:200)
12-08 17:32:25.474: WARN/BTLD(404): btapp_dm_pcm_carkit 00:0c:b6
12-08 17:32:25.474: WARN/BTLD(404): This is not btapp_dm_pcm_carkit
12-08 17:32:25.474: WARN/BTLD(404): btui_security_cback( LINK FAILURE:: hci status: 22) 8==link super to
12-08 17:32:25.474: WARN/BTLD(404): for bd_addr: x00:x0c:xb6:x01:x26:x2f
12-08 17:32:25.474: DEBUG/abtfilt(450): HCI_EVT_DISCONNECT event
12-08 17:32:25.484: DEBUG/BluetoothEventLoop(182): Device property changed:00:0C:B6:01:26:2F property:Disconnected
12-08 17:32:25.484: DEBUG/BluetoothAdapter(182): checkBluetoothAddress
12-08 17:32:25.484: INFO/BluetoothService(182): ACL disconnected, mAclLinkCount = 0
12-08 17:32:25.484: DEBUG/BluetoothA2dpService(182): Received intent Intent { act=android.bluetooth.device.action.ACL_DISCONNECTED (has extras) }
12-08 17:32:25.494: DEBUG/BluetoothAdapter(262): checkBluetoothAddress
12-08 17:32:25.494: DEBUG/BT HSHFP(262): Received intent Intent { act=android.bluetooth.device.action.ACL_DISCONNECTED (has extras) }
12-08 17:32:23.944: DEBUG/(404): jw_if_rfcomm_cl_cback: jw_if_rfcomm_cl_cback event=BTA_JV_RFCOMM_CLOSE_EVT
12-08 17:32:23.944: INFO/(404): jv_rfc_close: jv_rfc_close hdl 2, status 1, remotely initiated 0, result 19, port status 269483548
12-08 17:32:23.944: DEBUG/(404): jv_rfc_close: channel already disconnected
12-08 17:32:23.944: WARN/System.err(724): java.io.IOException: socket closed
蓝牙channel已经关闭,发生IO异常,read wrong是你System.out.println()出来的,这句是放在catch(IOException e)捕获块里
你可以参考下官方的蓝牙samples:http://developer.android.com/resources/samples/BluetoothChat/index.html
static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";//UUID
BluetoothAdapter mbtAdapt;
BluetoothSocket mbtSocket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetoothtest);
mbtAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
connectBluetooth("00:0C:B6:01:26:2F");
}
public void stop()
{
if (null == mbtAdapt)
return;
if (mbtAdapt.isDiscovering())
mbtAdapt.cancelDiscovery();
try {
if(mbtSocket != null)
{
mbtSocket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void connectBluetooth(String address)
{
mbtAdapt.cancelDiscovery();
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothDevice btDev = mbtAdapt.getRemoteDevice(address);
try {
mbtSocket = btDev.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mbtSocket.connect();
}
catch (IOException e) {
//TODO Auto-generated catch block
try {
mbtSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mbtSocket = null;
e.printStackTrace();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
stop();
}}
mbtSocket.connect();
}
catch (IOException e) {
//TODO Auto-generated catch block
try {
mbtSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mbtSocket = null;
e.printStackTrace();
}你关闭的时候,会引起这个异常操作,你注释掉mbtSocket = null;看看
@Override
public void onDestroy()
{
super.onDestroy();
stop();
}能够执行下去