蓝牙聊天功能

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;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值