I will not talk about the Bluetooth operation examples here. There are a lot of searches on the Internet, but they are usually developed according to the APIs on the official sdks.
I used the official API when I first started development, but there may be many strange problems. The most important thing is the same section.CodeRun on different machines, some machines are normal, and some machines throw exceptions.
For this reason, I have searched many methods. The most important thing is to use the reflection mechanism of Java.
For reflection mechanisms, refer to this blog http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html.
Bluetooth communication is very similar to General socket communication. It establishes a service socket and then the client establishes a connection. I will not explain how to use the official UUID method to establish it here.
The reflection mechanism is used here ..
Server:
Try {method mthd = ADP. getclass (). getmethod ("listenusingrfcommon", new class [] {Int. class}); try {Ss = (javasthserversocket) mthd. invoke (ADP, new object [] {29}); Java. lang. system. out. println ("service socket established successfully"); playthread. start ();} catch (illegalargumentexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (illegalaccessexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (invocationtargetexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} catch (securityexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (nosuchmethodexception e) {// todo auto-generated catch blocke. printstacktrace ();} // Ss = ADP. listenusinginsecurerfcommwithservicerecord ("suixiang", // my_uuid); // official API
Client:
// Effecthsocket S = device //. createinsecurerfcommsockettoservicerecord (descrithactivity. my_uuid); // s. connect (); // official apiblustmthsocket S = NULL; try {system. out. println ("START connection"); Method method = device. getclass (). getmethod ("createrfcommsocket", new class [] {Int. class}); try {S = (cmdthsocket) method. invoke (device, new object [] {29}); S. connect (); system. out. println ("connection successful");} catch (illegalargumentexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (illegalaccessexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (invocationtargetexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} catch (securityexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (nosuchmethodexception e) {// todo auto-generated catch blocke. printstacktrace ();}
The Connection established in this way is relatively stable .. Welcome to the discussion.