Android Bluetooth operation mainly solves connect failure

Source: Internet
Author: User

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.

Related Article

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.