private void startBt () {
listPairedBT = (ListView) findViewById(R.id.listPairedBT);
listPairedBT.setOnItemClickListener(this);
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
btStatus = (TextView) findViewById(R.id.tvBtStatus);
listPairedBT.setAdapter(listAdapter);
btAdapter = BluetoothAdapter.getDefaultAdapter();
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new BroadcastReceiver()
{
public void onReceive(android.content.Context context, Intent intent) {
String action = intent.getAction();
System.out.println("123456789");
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println("123456789 " + device.getName()+" "+" "+ "\n"+device.getAddress());
String s = "";
for(int a=0; a < pairedDevices.size(); a++)
{
if(device.getName().equals(pairedDevices.get(a)))
{
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName()+" "+s+" "+ "\n"+device.getAddress());
}
else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
{
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
//btAdapter.cancelDiscovery();//added to test
}
else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action))
{
if(btAdapter.getState()==btAdapter.STATE_OFF)
{
turnOnBluetooth();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
btnPreference.setOnClickListener(this);
if(btAdapter==null)
{
Toast.makeText(getApplicationContext(), "Bluetooth does not support!", 0).show();
finish();
}
else
{
if(!btAdapter.isEnabled())
{
turnOnBluetooth();
}
getPairedDevices();
startDiscovery();
}
mHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what)
{
case SUCCESS_CONNECT:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
btStatus.setText("Connected");
connectedThread.start();
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String btReceivedTemp = new String(readBuf, 0, msg.arg1); // create string from bytes array
stringBuilder.append(btReceivedTemp); // append string
int endOfLineIndex = stringBuilder.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String btReceived = stringBuilder.substring(0, endOfLineIndex); // extract string
stringBuilder.delete(0, stringBuilder.length()); // and clear
btStatus.setText("Data from Arduino: " + btReceived); // update TextView
//if accident string got caught, call accident % method
// check the event type and add event to the event manager
if (btReceived.equalsIgnoreCase("1")) { // switch event
eventsManager.addEvent("switch_pressed");
} else if (btReceived.equalsIgnoreCase("kaboom")) { // vibration sensor event
eventsManager.addEvent("vibration_sensor");
}
}
}
}
};
}
我将代码放入startBt方法中,因此我可以在onCreate()和OnResume()中调用它。 当我从另一个活动返回活动但是它不更新列表视图时,蓝牙连接会接收数据。它保持空白
答案 0 :(得分:0)
在onActivityResult()
方法中,您需要致电listPairedBT.notifyDataSetChanged()
。
答案 1 :(得分:0)
您需要致电
活动结束后,您的列表视图notifyDataSetChanged()
。