Android Bluetooth: Connect()/Disconnect() - android

I currently am designing an app which needs to connect to a device, write/read data, and close the connection reliably. Currently I have the write/read solid. My disconnect and then reconnect is terribly unreliable, and often actually crash the phone.. and sometimes Eclipse. I've been looking through numerous articles trying to figure it out and.. no luck..
****CONNECT FUNCTION**
public boolean connect()
{
ConfigData.getInstance();
BluetoothSocket tmp = null;
BluetoothDevice device = ConfigData.m_SharedBluetoothDevice;
Method m;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);//(BluetoothSocket)
m.invoke(device, 1);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
ConfigData.m_SharedBluetoothSocket = tmp;
try {
ConfigData.m_SharedBluetoothSocket.connect();
ConfigData.bIsBTConnected = true;
} catch (IOException e) {
try {
closeSocket();
m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e.printStackTrace();
} catch (IllegalAccessException e1) {
e.printStackTrace();
} catch (InvocationTargetException e1) {
e.printStackTrace();
}
ConfigData.m_SharedBluetoothSocket = tmp;
try {
ConfigData.m_SharedBluetoothSocket.connect();
ConfigData.bIsBTConnected = true;
} catch (IOException e1) {
ConfigData.m_BluetoothException += e1.toString();
ConfigData.bIsBTConnected = false;
return false;
}
e.printStackTrace();
return true;
}
return true;
}
****Disconnect Function**
public void destroySocket()
{
try {
if(m_InStream != null)
{
m_InStream.close();
m_InStream = null;
}
if(m_OutStream != null)
{
m_OutStream.close();
m_OutStream = null;
}
if(ConfigData.m_SharedBluetoothSocket != null)
{
ConfigData.m_SharedBluetoothSocket.close();
ConfigData.m_SharedBluetoothSocket = null;
}
if(m_InStream == null && m_OutStream == null && ConfigData.m_SharedBluetoothSocket == null)
{
ConfigData.bIsBTConnected = false;
}
} catch (IOException e1) {
m_InStream = null;
m_OutStream = null;
ConfigData.m_SharedBluetoothSocket = null;
e1.printStackTrace();
}
}
So the disconnect is successful and returns everything null. The problem is that when I reconnect it blocks at the 2nd connect attempt and will either just sit there or completely crash the phone, causing several reboots.
Does anyone have any advice here? Its been extremely frustrating. Any help would be much appreciated!!
Thanks and Gig 'Em!
TxAg

What phone are you using? What OS? See this answer:
Disconnect a bluetooth socket in Android
The close is actually not working properly on some HTC 2.1update1 phones

Related

NumberPicker Restrictions on non-SDK interfaces

I'm trying to create a custom NumberPicker like this.
Class<?> numberPickerClass = null;
try {
numberPickerClass = Class.forName("android.widget.NumberPicker");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field selectionDivider = null;
try {
if (numberPickerClass != null) {
selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
if (selectionDivider == null) {
return;
}
try {
selectionDivider.setAccessible(true);
selectionDivider.set(this, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
However, starting from API29, Android is restricting access on non-SDK interfaces. They are suggesting using set/getDividerHeight instead. But I'm super new to Android and I'm not sure how to update my code. Can anyone kindly help? Thanks!

Stored procedure in Android with MySQL-connector-Java-3.0.17-ga- bin doesn't work

I have an issue when I try to call a stored procedure in Android, below is my code.
private List<LocationDO> search() {
Connection connection = null;
CallableStatement callableStatement = null;
try {
connection = getConnection();
if (connection != null) {
callableStatement = connection.prepareCall("{call cy_sp_getLocation(?,?,?)}");
callableStatement.setFloat(1, 48.782818f);
callableStatement.setFloat(2, 24.62229f);
callableStatement.setFloat(3, 0.5f);
callableStatement.execute();
}
} catch (DAOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
When execute this line
callableStatement = connection.prepareCall ("{call cy_sp_getLocation (?,?,?)"); throw "Callablestatments not supported.".
I used the connector for Android MySQL-connector-Java-3.0.17-ga-bin.jar.

How can i save data from multipart/form-data in servlet and save this data to db?

public void saveData(HttpServletRequest request,HttpServletResponse response) throws IOException {
try {
boolean checkMultiPart = ServletFileUpload.isMultipartContent(request);
System.out.println(checkMultiPart);
//fin = request.getInputStream();
Part part = request.getPart("pic");
fin = part.getInputStream();
System.out.println(fin);
writer = response.getWriter();
if(fin != null){
fin.read();
System.out.println("Inside Function");
query = "INSERT INTO SAVEPIC(picID,PIC) VALUES (picIncVal.nextval,?)";
Class.forName(DB_DRIVER);
connection = DriverManager.getConnection(DB_URL, DB_USER,DB_PASSWORD);
connection.setAutoCommit(false);
insertStatement = connection.prepareStatement(query);
insertStatement.setBinaryStream(1, fin, fin.available());
row = insertStatement.executeUpdate();
connection.commit();
System.out.println("Save To Data Base");
}
if (row > 0)
writer.print("SUCCESS");
else
writer.print("FAILED");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (NullPointerException e) {
writer.print("Pic Value is Null");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fin != null)
fin.close();
if(writer != null)
writer.close();
if (connection != null) {
insertStatement.close();
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
I want to store image which i am sending from android in multipart/form-data. Can any one give me suggestions how can i do it.
By this code only some bits are storing into the db, Not whole image.
Thanks in advance.

Android - Bluetooth connection InputStream close crashes my app

I have an Android application where I connect to a bluetooth device. When I shut the device off, the application tries to disconnect. So I try to close the inputstream the outputstream and the socket. Even though I have a try catch on each close try, the app crashes when it tries to close the inputstream. Any ideas? Code example:
private void resetConnection() {
if (mmInputStream != null) {
try {
mmInputStream.close();
} catch (Exception e) {
Log.e(LOG_TAG, "CANNOT CLOSE InputStream", e);
}
mmInputStream = null;
}
if (mmOutputStream != null) {
try {
mmOutputStream.close();
} catch (Exception e) {
Log.e(LOG_TAG, "CANNOT CLOSE OutputStream", e);
}
mmOutputStream = null;
}
if (mmSocket != null) {
try {
mmSocket.close();
} catch (Exception e) {
Log.e(LOG_TAG, "CANNOT CLOSE mmSocket", e);
}
mmSocket = null;
}
}
Any suggestions? Thanks

data not sent using bluetooth socket (android)

I am trying to send a file using Android bluetooth sockets. I am getting the pairing requests but unfortunately the data is not being transferred
my Server side and client side is as follows:
Server: (for my application server is receiving the data)
public void run() {
super.run();
BluetoothAdapter mBtadapter = BluetoothAdapter.getDefaultAdapter();
Log.i("bluetooth Adapter", "got adapter "+ mBtadapter.getAddress());
try {
Ssocket = mBtadapter.listenUsingRfcommWithServiceRecord("CardXchange", uuid);
Log.i("Socket", "Socket Acquired "+ Ssocket.toString());
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(read_flag()==1){
try {
Toast.makeText(c, "flag is eventually 1", Toast.LENGTH_LONG).show();
Ssocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
client_socket = Ssocket.accept();
if(client_socket!=null){
//this function is used to handle the connection when sockets get connected
manageConnection1(client_socket);
Ssocket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("breaked ", "oh god!! out of the loop ");
}
//implementation of the function
private void manageConnection1(BluetoothSocket client_socket2) {
// TODO Auto-generated method stub
int bytes;
Log.i("serevr chya manage connection madhe gela", "in servers manage connection ");
File file = new File("/sdcard/myfolder/received.Xcard");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
InputStream is = client_socket2.getInputStream();
while((bytes = is.read(buffer, 0, 1024))>0){
fos.write(buffer, 0, 1024);
}
is.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
client_socket2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and the client side (the sending side is like this):
public void run(){
try {
Method m = send_dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
Csocket = (BluetoothSocket) m.invoke(send_dev, 1);
Csocket.connect();
manageSendConnection(Csocket);
}catch(Exception e){
e.printStackTrace();
}
}
//function to handle after connection is established.
private void manageSendConnection(BluetoothSocket csocket) {
// TODO Auto-generated method stub
File f = new File("/sdcard/myfolder/card3.Xcard");
byte[] buffer = new byte[(int)f.length()];
try {
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(buffer, 0, (int)f.length());
OutputStream os = csocket.getOutputStream();
os.write(buffer);
os.close();
fis.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
csocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am getting the pairing request but the data is not being sent.
can anyone please help me to find the problem and give a hint on solution??

Categories

Resources