Bluetooth chat software (emoticons and voice support)

Source: Internet
Author: User

First of all, this is a revision from the official demo.

First: Comments on the official demo

1. The meaning of the three thread used:
(1) Acceptthread
Server-side, play the role of monitoring. (Accept function)
(2) Connectthread
This thread runs and attempting to making an outgoing connection with a device. (Attempting to connect)
(3) Connectedthread
This thread is runs during a connection with a remote device. It handles all incoming and outgoing transmissions. (already connected, ready for data exchange)

2. Process
The bluetoothchat loads while the Bluetoothchatservice is started.
(1) Click on the "Sight Mirror" icon in the upper right corner, call ensurediscoverable, so that you can be detected by the surrounding Bluetooth
(2) Click on the "Search" icon in the top right corner to enter devicelistactivity. It is actually a dialog box that shows the paired Bluetooth and a button. Click button to call Dodiscovery to search around the Bluetooth device while making yourself invisible.
Click any item in the Bluetooth list to respond to Onitemclicklistener and pass the required information back to Bluetoothchat via intent. Meanwhile, Devicelistactivity registers a broadcastreceiver to detect if there are new open bluetoothdevice around
(3) bluetoothchat response Onactivityresult, call Connectdevice. Get the intent data passed in and call Bluetoothchatservice's Connect method right away.
In this method, cancel the thread that is trying to connect or is already connected. Then restart an attempt to connect to the thread (Connectthread) and set the status bit to get a response at Bluetoothchat's Mhandler
4.ConnectThread inside Mmsocket.connect (); indicates connected. If it fails, call connectionfailed and return. If successful, call connected (Mmsocket, Mmdevice, Msockettype).
The connectionfailed function passes the "failed" message to handler to the Bluetoothchat to display, and then calls BluetoothChatService.this.start () cancels the thread that is trying to connect or is already connected. Then restart a acceptthread thread to continue listening (accept).
Connected (Mmsocket, Mmdevice, Msockettype) cancels all threads, turns on connectedthread for data transfer, and sends a "connection success" message through handler to Bluetoothchat.
5.ConnectedThread inside is IO and data transmission. There may be Bluetooth off-line during transmission. Call Connectionlost, restart Bluetoothchatservice

Second: New features

(1) Make the soft keyboard hidden by clicking on a section other than EditText
Setupui (Findviewbyid (R.id.root));//define Main.xml the largest linearlayout id=root
function function: Traverse all controls, including child controls, to hide the soft keyboard if the view is not edittext

(2) expression transmission
Initfaceview ();
Using reflection mechanism to "load pictures by file name"
The image information is passed to the other end in the form of binary data, and the other end is given a regular expression to determine whether there is an expression in the message.
Spannablestring spannablestring = expressionutil.getexpressionstring (context, str, zhengze);

(3) voice transmission
The data transmission section, the official method for voice does not fit.

bytenewbyte[1024];bytes = mmInStream.read(buffer);

The official 1024 cache is enough for characters, but not enough for voice.
Bluetooth transmission speed is limited, a few seconds of voice will be divided several times, so to read it all again to judge
So you need to modify the Run method in Connectedthread. Also remember that you cannot end the Run method after you have finished getting all the data. Because there's still data transmission.

public void Run () {log.i (TAG,"BEGIN mconnectedthread"); byte[] buffer = new byte[1024x768];intbytes =0; String Source ="";//Get all the data//official gives the method for the voice does not fit. Bluetooth transmission speed is limited, a few seconds of the voice will be divided several times//So to read it all again to judge while(true) {try {bytes = Mminstream.Read(buffer); String temp = new String (buffer,0, bytes);                    SOURCE + = temp; LOG.I ("bytes", Bytes +"");//This one989is through the backstageLogLook, what exactly is this value, I don't know.if(Bytes <989) {//Voiceif(Source.contains ("Recorder")) {//Construction RecorderintEnd = Source.lastindexof ("]");intStart = Source.indexof ("[") +1; String[] Sub = source.substring (start, end). Split (",");                            intTempinteger = Sub[0].indexof ("=") + 1;String content = Sub[0].substring (Tempinteger, sub[0].length ());float audiolength = Float.parsefloat ( Sub[1]. Split ("=") [1]);String FilePath = Sub[2].split ("=") [1];Recorder Recorder = new Recorder (content, Audiolength, FilePath,                            Bluetoothchat.voice_read); Mhandler.obtainmessage (Bluetoothchat.voice_read, Bytes,-1, recorder). Sendtotarget (); }Else{//Message Mhandler.obtainmessage (Bluetoothchat.message_read, b Ytes,-1, buffer). Sendtotarget (); } Source =""; }} catch (IOException e) {log.e (TAG,"Disconnected", e); Connectionlost ();//Start the service over to restart listening mode BluetoothChatService.this.start ();return; }            }        }

Note that the above code has a bug. Voice is not sound after transmission because the voice is not encoded and decoded. Only binary data is used. String type decoding by binary encoding can also be restored, voice not
Attached Source: http://download.csdn.net/detail/lxj1137800599/9514013

Bluetooth chat software (emoticons and voice support)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.