public void onClick(View view){
showToast("开始扫描");
list.clear();
list.addAll(BluetoothUtils.getInstance(mContext).getAvailableDevices());
adapter.notifyDataSetChanged();
BluetoothUtils.getInstance(this).scanDevices();
}
private void initReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothMessage.ACTION_INIT_COMPLETE);
filter.addAction(BluetoothMessage.ACTION_CONNECTED_SERVER);
filter.addAction(BluetoothMessage.ACTION_CONNECT_ERROR);
registerReceiver(receiver, filter);
}
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()){
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
for(int i=0;i<list.size();i++){
if(device.getAddress()==null || device.getAddress().equals(list.get(i).getAddress())){
return;
}
}
list.add(device);
adapter.notifyDataSetChanged();
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
// showToast("开始扫描");
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
// showToast("扫描完成");
break;
case BluetoothMessage.ACTION_INIT_COMPLETE:
ProgressUtils.dismissProgress();
break;
case BluetoothMessage.ACTION_CONNECTED_SERVER:
ProgressUtils.dismissProgress();
String remoteAddress = intent.getStringExtra(BluetoothUtils.EXTRA_REMOTE_ADDRESS);
openChatRoom(remoteAddress);
break;
case BluetoothMessage.ACTION_CONNECT_ERROR:
ProgressUtils.dismissProgress();
showToast(intent.getStringExtra(BluetoothUtils.EXTRA_ERROR_MSG));
break;
}
}
};
private void openChatRoom(String remoteAddress) {
Intent intent = new Intent(mContext,ChatActivity.class);
intent.putExtra("remoteAddress",remoteAddress);
startActivity(intent);
}
/**
* Created by xuhuan .
*/
public class DBManager {
private static DbManager.DaoConfig daoConfig;
public static DbManager.DaoConfig getDaoConfig(){
if(daoConfig==null){
daoConfig = new DbManager.DaoConfig();
daoConfig.setDbVersion(2);
}
return daoConfig;
}
public static void save(Object obj){
try {
x.getDb(DBManager.getDaoConfig()).save(obj);
} catch (DbException e) {
e.printStackTrace();
}
}
public static List<BluetoothMessage> findAll(String remoteAddress) {
try {
Selector<BluetoothMessage> selector = x.getDb(getDaoConfig()).selector(BluetoothMessage.class);
selector.where("sender","=",remoteAddress);
selector.or("receiver","=",remoteAddress);
return selector.findAll();
} catch (DbException e) {
e.printStackTrace();
}
return null;
}
}