简体   繁体   中英

Android - How to close a bluetooth socket/port left open by a killed program

I have been running into an issue with my product that has a rooted tablet (iball 3G 7271,running on 4.1.2) which is trying to communicate with an RN42 Bluetooth module.As per my project requirement the android app, which initiates the BT connectivity to the RN42 must have capability to:

  1. Be updated remotely
  2. Should restart itself in case if it crashes.Both these functionality have been coded and found working.

However as you know in both these cases the Bluetooth connectivity is disrupted and lost,and has to be re-established between the tablet and the RN42.

The problem I am facing is that,when the app which was connected via Bluetooth to RN42(is either updated/restarted as per case 1 or case2) leaves the bluetooth socket/port in the connected/open state itself,whilst the new app is re-installed/restarted respectively.Hence the newly re-installed/restarted app, when it initiates a bluetooth connection with the RN42, the device OS allocates it a new bluetooth socket/port(and not the older one since it is still occupied). But after a couple of re-installs/restarts(20 to be precise) the device runs out of any available socket port which it can allocate to the app and I get repeated exception stating .

Is there a way that I can close the bluetooth socket/port which was left open by an app prior to its reinstall/restart?

I had such issues with different BT chipset (mainly from lowcost tablet) and switching on/off the Bluetooth adapter did the job, through .enable() or .disabled().

Notice it is a bad idea, bad practice, but in the scope of my mockup it was usefull for debug :)

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();        
mBluetoothAdapter.disable();
while(mBluetoothAdapter.isEnabled());
mBluetoothAdapter.enable();
while(!mBluetoothAdapter.isEnabled());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM