Android Studio實驗作業之藍牙聊天功能

今兒給大夥講個笑話
有一天,我寫了一個項目(依託四方大佬救濟的那種寫)
整完了之後跑起來是這樣在這裏插入圖片描述
經過各種調試之後,運行了之後是這樣
在這裏插入圖片描述
我就納悶,難道是哪裏出錯了?
沒道理啊?
後來找到了大佬,大佬跑了一遍,沒問題,,,
後來大佬一機靈,在真機上面跑了一遍
成了。。。
所以這個模塊是沒辦法在虛擬機上面跑了,大夥有興趣的可以下載之後在手機上面跑一跑(對,我真的沒手機。。。家裏窮。。。用的用的都是iPhone7及以下版本 別噴我不愛國 真和愛國沒什麼關係)

好了 廢話到這裏就結束了

下面說說項目吧
首先是必不可少是權限申請
這裏百度了不少,肯定不是最精煉的,但差不多是最全的了,能用的不能用的全部放上去了
src/main/AndroidManifest.xml 中插入mainfest標籤下

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

然後就是src/main/java/cn/com/lenew/bluetooth/activity/MainActivity.java
沒有別的,主要還是一個對各個類的調用,控制顯示之類的
僅附上部分代碼

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

隨後就是藍牙通信的協議問題,這個相對來說就比較複雜了
這裏說一下 ,我使用的是BluetoothSocket和BluetoothServerSocket
src/main/java/cn/com/lenew/bluetooth/utils/BluetoothUtils.java

/**
 * Created by xuhuan 0007.
 */
public class BluetoothUtils {

    private static final String PROTOCOL_SCHEME_RFCOMM = "server_name";
    private static final String UUIDString = "00001101-0000-1000-8000-00805F9B34FB";
    public static final String EXTRA_REMOTE_ADDRESS = "remoteAddress";
    public static final String EXTRA_ERROR_MSG = "error_msg";
    private static BluetoothUtils instance;

    /** 已連接到服務器 */
    private static final int CONNECTED_SERVER = 1;
    /** 連接服務器出錯 */
    private static final int CONNECT_SERVER_ERROR = CONNECTED_SERVER + 1;
    /** 正在連接服務器 */
    private static final int IS_CONNECTING_SERVER = CONNECT_SERVER_ERROR + 1;
    /** 等待客戶端連接 */
    private static final int WAITING_FOR_CLIENT = IS_CONNECTING_SERVER + 1;
    /** 已連接客戶端 */
    private static final int CONNECTED_CLIENT = WAITING_FOR_CLIENT + 1;
    /** 連接客戶端出錯 */
    private static final int CONNECT_CLIENT_ERROR = CONNECTED_CLIENT + 1;

    private BluetoothAdapter bluetoothAdapter;
    private Context mContext;

    /**
     * socket集合
     */
    private HashMap<String, BluetoothSocket> socketMap = new HashMap<>();
    /**
     * 遠程設備集合
     */
//    private HashMap<String, BluetoothDevice> remoteDeviceMap = new HashMap<>();

    private HashMap<String,ReadThread> readThreadMap = new HashMap<>();

    private BluetoothServerSocket mServerSocket;

    private Handler linkDetectedHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if(msg.obj instanceof BluetoothMessage){
                BluetoothMessage message = (BluetoothMessage) msg.obj;
                Intent intent = new Intent();
                intent.setAction(BluetoothMessage.ACTION_RECEIVED_NEW_MSG);
                intent.putExtra("msg", message);
                mContext.sendOrderedBroadcast(intent, null);

                DBManager.save(message);
//                mContext.sendBroadcast(intent);
            }else {
                Intent intent = new Intent();
                switch (msg.what){
                    case WAITING_FOR_CLIENT:
                        //初始化服務器完成
                        intent.setAction(BluetoothMessage.ACTION_INIT_COMPLETE);
                        break;
                    case IS_CONNECTING_SERVER:
                        //正在連接服務器

                        break;
                    case CONNECTED_CLIENT:
                        //有客戶端連接到自己

                        break;
                    case CONNECT_CLIENT_ERROR:
                        //連接客戶端出錯

                        break;
                    case CONNECT_SERVER_ERROR:
                        //連接服務器出錯
                        intent.putExtra(EXTRA_ERROR_MSG,(String)msg.obj);
                        intent.setAction(BluetoothMessage.ACTION_CONNECT_ERROR);
                        break;
                    case CONNECTED_SERVER:
                        intent.putExtra(EXTRA_REMOTE_ADDRESS,(String)msg.obj);
                        intent.setAction(BluetoothMessage.ACTION_CONNECTED_SERVER);
                        break;

                }
                mContext.sendBroadcast(intent);
//                String msgContent = (String) msg.obj;
//                Toast.makeText(mContext, msgContent, Toast.LENGTH_SHORT).show();
            }

        }
    };


    private BluetoothUtils(Context context) {
        mContext = context;
        if (context == null) {
            throw new RuntimeException("Parameter context can not be null !");
        }
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public static BluetoothUtils getInstance(Context context) {
        if (instance == null) {
            instance = new BluetoothUtils(context);
        }
        return instance;
    }

    public void enableBluetooth() {
        if (!bluetoothAdapter.isEnabled()) {
//            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//            intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
//            mContext.startActivity(intent);
            bluetoothAdapter.enable();
        }
    }

    public void scanDevices() {
        if (!bluetoothAdapter.isEnabled()) {
            enableBluetooth();
            return;
        }
        bluetoothAdapter.startDiscovery();
        startService();
    }

    private void startService() {
        Intent serverIntent = new Intent(mContext, ServerService.class);
        mContext.startService(serverIntent);

        Intent clientIntent = new Intent(mContext, MessageService.class);
        mContext.startService(clientIntent);
    }

    public boolean isEnabled() {
        return bluetoothAdapter.isEnabled();
    }

    public ArrayList<BluetoothDevice> getAvailableDevices() {
        Set<BluetoothDevice> availableDevices = bluetoothAdapter.getBondedDevices();
        ArrayList availableList = new ArrayList();
        for (Iterator<BluetoothDevice> iterator = availableDevices.iterator(); iterator.hasNext(); ) {
            availableList.add(iterator.next());
        }
        return availableList;
    }

    public boolean isBonded(BluetoothDevice device) {
        Set<BluetoothDevice> availableDevices = bluetoothAdapter.getBondedDevices();
        for (Iterator<BluetoothDevice> iterator = availableDevices.iterator(); iterator.hasNext(); ) {
            if (device.getAddress().equals(iterator.next().getAddress())) {
                return true;
            }
        }
        return false;
    }

    public boolean isDiscoverying() {
        return bluetoothAdapter.isDiscovering();
    }

    public void cancelScan() {
        bluetoothAdapter.cancelDiscovery();
    }


    //開啓客戶端
    private class ClientThread extends Thread {

        private String remoteAddress;

        public ClientThread(String remoteAddress) {
            this.remoteAddress = remoteAddress;
        }

        @Override
        public void run() {
            try {
                //創建一個Socket連接:只需要服務器在註冊時的UUID號
                // socket = device.createRfcommSocketToServiceRecord(BluetoothProtocols.OBEX_OBJECT_PUSH_PROTOCOL_UUID);
                BluetoothDevice device = bluetoothAdapter.getRemoteDevice(remoteAddress);
                BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString(UUIDString));
//                socketMap.put(remoteAddress, socket);
                //連接
                Message msg2 = new Message();
                msg2.obj = "請稍候,正在連接服務器:" + remoteAddress;
                msg2.what = IS_CONNECTING_SERVER;
                linkDetectedHandler.sendMessage(msg2);

                socket.connect();

//                socketMap.put(BluetoothMessage.bluetoothAddress, socket);
                socketMap.put(remoteAddress, socket);

                Message msg = new Message();
//                msg.obj = "已經連接上服務端!可以發送信息。";
                msg.obj = remoteAddress;
                msg.what = CONNECTED_SERVER;
                linkDetectedHandler.sendMessage(msg);
                //啓動接受數據
                ReadThread mreadThread = new ReadThread(remoteAddress);
                readThreadMap.put(remoteAddress,mreadThread);
                mreadThread.start();
            } catch (IOException e) {
                e.printStackTrace();
                socketMap.remove(remoteAddress);
                Log.e("connect", e.getMessage(), e);
                Message msg = new Message();
                msg.obj = "連接服務端異常!斷開連接重新試一試。"+e.getMessage();
                msg.what = CONNECT_SERVER_ERROR;
                linkDetectedHandler.sendMessage(msg);

//                remoteDeviceMap.remove(remoteAddress);
            }
        }
    }


    //開啓服務器
    private class ServerThread extends Thread {
        @Override
        public void run() {

            try {
                    /* 創建一個藍牙服務器
                     * 參數分別:服務器名稱、UUID   */
                mServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM, UUID.fromString(UUIDString));


                while (true){
                    Log.d("server", "wait cilent connect...");

                    Message msg = new Message();
                    msg.obj = "請稍候,正在等待客戶端的連接...";
                    msg.what = WAITING_FOR_CLIENT;
                    linkDetectedHandler.sendMessage(msg);

                    /* 接受客戶端的連接請求 */
                    BluetoothSocket socket = mServerSocket.accept();
                    socketMap.put(socket.getRemoteDevice().getAddress(), socket);
//                    remoteDeviceMap.put(socket.getRemoteDevice().getAddress(),socket.getRemoteDevice());
                    Log.d("server", "accept success !");

                    Message msg2 = new Message();
                    String info = "客戶端已經連接上!可以發送信息。";
                    msg2.obj = info;
                    msg.what = CONNECTED_CLIENT;
                    linkDetectedHandler.sendMessage(msg2);
                    //啓動接受數據
                    ReadThread mreadThread = new ReadThread(socket.getRemoteDevice().getAddress());
                    readThreadMap.put(socket.getRemoteDevice().getAddress(),mreadThread);
                    mreadThread.start();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /* 停止服務器 */
//略




    /* 停止客戶端連接 */
//略
    //發送數據
//略

    //讀取數據
//略

當然 這個模塊到這裏肯定是不算結束的,還有部分類,我就不一一展示了,末尾我會附上源代碼

接下來是通信服務:
src/main/java/cn/com/lenew/bluetooth/service/MessageService.java:

/**
 * Created by xuhuan  0010.
 */
public class MessageService extends Service{
    private Context mContext;
    private NotificationManager notificationManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        IntentFilter intentFilter = new IntentFilter(BluetoothMessage.ACTION_RECEIVED_NEW_MSG);
        intentFilter.setPriority(990);
        registerReceiver(msgReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(msgReceiver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }


    public void sendNotification(String content,String remoteAddress,BluetoothMessage message){

        Intent chatIntent = new Intent(mContext, ChatActivity.class);
        chatIntent.putExtra("remoteAddress",remoteAddress);
        chatIntent.putExtra("lastmsg",message);


        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(mContext)
                .setContentText(content)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.icon_logo)
                .setAutoCancel(true)
                .setContentTitle("您有一條新消息")
                .build();


        notification.defaults = Notification.DEFAULT_SOUND;
        notificationManager.notify(1,notification);
    }


    BroadcastReceiver msgReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BluetoothMessage bluetoothMessage = (BluetoothMessage) intent.getSerializableExtra("msg");
            bluetoothMessage.setIsMe(0);

            String remoteAddress = bluetoothMessage.getSender();

            String msgContent;
            if(bluetoothMessage.getContent().length()>10){
                msgContent = bluetoothMessage.getContent().substring(0,10);
            }else {
                msgContent = bluetoothMessage.getContent();
            }
            String msg = bluetoothMessage.getSenderNick()+":"+msgContent;

            sendNotification(msg,remoteAddress,bluetoothMessage);
        }
    };
}

還有數據服務:
src/main/java/cn/com/lenew/bluetooth/database/DBManager.java:

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

差不多就是以上了,至於佈局還有其他的,這裏也就不繼續展開了 需要的可以自行下載,當然 先提前聲明,代碼絕對不是最簡單或者說完美的,很多地方我也是在網上down的,有些部分或許根本沒有用到。反正是修修補補的,大概差不多就是這麼個原理。
那麼最後 附上碼雲地址:項目源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章