Connecting to Mac OSX using an Android phone - android

I am writing some code to connect my Android device to my Mac using bluetooth. Its seems to create a connection,(i am able to see my android in tbe connected list in Mac) But it throws an exception on the android device.
BluetoothDevice bd = mBluetoothAdapter.getRemoteDevice("00:26:08:BF:D0:22");
BluetoothSocket bs = null;
try {
bs = bd.createRfcommSocketToServiceRecord(DeviceSearchService.MY_UUID);
try {
bs.connect();
} catch (IOException ex) {
Log.i(TAG, "Exception in connecting to socket." + ex.getMessage(), ex);
}
}
The bs.connect is throwing an exception saying cannot connect to the device. The device in question is a Mac OSX laptop of mine. I am running this code from Galaxy S device.
THe UUID used is "00001101-0000-1000-8000-00805F9B34FB"
Am I missing something ?

have you declared the Bluetooth permissions in your application manifest file?
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>

Related

Trigger UIAutomator Test Script programmatically

We are using UIAutomator for automation.
To run UIAutomator test scripts we used to compile the scripts from command prompt by using either USB or Wifi adb and it will run on the android device.
UiAutomator scripts will come in jar format after compilation.
We will push the jar in the device and we have trigger the test scripts.
The command to start the test script in “adb shell uiautomator runtest TestPackage.jar”.
It is working with normal adb connection.
But in our case we have to initiate the test scripts without any adb connection.
So we tried by passing the commands programmatically in the device using this java code
try {
StringBuffer output = new StringBuffer();
Process p = Runtime.getRuntime().exec(params[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
p.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
After the execution we are getting the following exception:
java.lang.securityException : longMsg = You do not have android.permission.RETRIEVE_WINDOW_CONTENT required to call registerUITestAutomationService
If we run scripts from command prompt with adb connection it is working. But if we tried the command programmatically we are getting this exception.
We came to know that the above mentioned issue can be overcome in a rooted android device by enabling the RETRIEVE_WINDOW_CONTENT permission. But we want this scripts to run on a non-rooted setup.
Can anyone help me in explaining this security permission issue?
How can I resolve it?

File exchange PC <--> Android via USB MTP issues

In my android app, I create a file and write some test text into it:
File externalPath = getExternalFilesDir(null);
File importPath = new File(externalPath, "pd-import");
if(!importPath.exists()) {
Log.d(this.getClass().getSimpleName(), "Create import dir: " + importPath.getAbsolutePath());
importPath.mkdirs();
}
File readme = new File(importPath, "README.txt");
try {
FileWriter fw = new FileWriter(readme);
fw.write("This is a test");
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
This writes the file, which can be approved with an android file browser like B1 File Manager. It's written to HOME/Android/data/JAVA_PACKAGE_NAME/files/pd-import/README.txt
The logcat shows me:
D/MainActivity﹕ Create import dir: /storage/emulated/0/Android/data/JAVA_PACKAGE_NAME/files/pd-import
When I connect my Nexus, where I tested the code, to my Ubuntu Laptop via USB, I see all the other applications data directories like NEXUS 5/Interner Speicher/Android/data/ALL_THE_OTHER_JAVA_PACKAGE_NAMEs ("Interner Speicher" stands for: internal memory). But the folder is not listed for my app is not listed.
Do I need to set some additional medatdata/information/whatever, to have the folder listed over the MTP connection? Anny suggestions?
Does the directory appear when the device is rebooted ? If yes, I think this bug is the cause : https://code.google.com/p/android/issues/detail?id=38282
All phones using MTP instead of USB Mass storage do not properly show
the list of files when that phone is connected to a computer using a
USB cable. Android apps running on the device also cannot see these
files.
This affects files written using Java APIs, but does not appear to
affect files written using the C API.
The solution is to reboot the device.
It does not seem to be resolved.

Android File open failed: EROFS

Sometimes I get Exception.java.io.FileNotFoundException, file open failed: EROFS ( Read- only file System) . I have WRITE_EXTERNAL_STORAGE permission in my manifest, once in a while when I have my services running then I get these exceptions and few operations will not be succeeded but if I reboot my device then it works fine.
I want to catch "Exception.java.io.FileNotFoundException" exception and reboot my device programmatically. My device is rooted one so I programmatically execute "su -c reboot".
I need to know how to catch " File open failed: EROFS " exception and trigger a reboot ??
Will solve your problem this:
try{
do whatever
}catch(FileNotFoundException ex){
if("EROFS ( Read- only file System)".equals(ex.getMessage())){
call code for reboot
}
}
You can replace the error message or even skip it
Edit: - based on comment
try{
do whatever
}catch(IOException ex){
if((ex instanceof FileNotFoundException) && "EROFS ( Read- only file System)".equals(ex.getMessage())){
call code for reboot
}
}

How can i connect to sql server from my android application?

I'm developing an android application. I need connect to sql server. I wrote a code for test connection but i get this error :
java.sql.SQLException: Network error IOException: Permission denied
I checked connection string.
I closed firewall for 1433.
I tried configured Sql Server tcp and remote access settings
but i still get the same error. What is my mistake?
My environment:
Sql Server 2008 R2
Eclipse
jtds
My Code is:
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
**Connection conn=DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.33:1433/Android_Deneme;instance=CASPER\\MORTY;user=ugur;password=1234");**
String sql="insert into tablo1(ad,yas) values(?,?)";
PreparedStatement prepared = conn.prepareStatement(sql);
prepared.setString(1, "ali");
prepared.setInt(2, 30);
prepared.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
You have to add the following permission to your application manifest file.
<manifest>
.....
<uses-permission android:name="android.permission.INTERNET" />
.....
</manifest>

Android Log vs Titanium INFO

while programming android with eclipse, no matter if you're using the emulator or a device connected in debug mode you can easily check the Log for looking at the custom messages that you wrote.
e.g.: Log.i("foo: " + foo);
With the method:
Titanium.API.info("foo: " + foo);
if i'm using the emulator it all works fine, but if i'm deploying on the device is there a way of looking at the INFO?? with TiStudio or even TiDev
Thanks
You can use adb from the android SDK tools directory to see your Titanium Titanium.API.info calls:
tools/adb logcat | grep "TiAPI"
This will filter the adb log, giving you a cleaner view of only your log messages.
You can see logcat from the current device. Enable debug mode on the device, and connect it to the computer.
Then, if using eclipse, on the DDMS view on Devices select the device you want and the LogCat will show logs from it.
You can also see with this app the logs:
https://market.android.com/details?id=org.jtb.alogcat
Or save them to file:
try {
File filename = new File(Environment.getExternalStorageDirectory()
+ "/logfile.txt");
filename.createNewFile();
String[] cmd = new String[] { "logcat", "-v", "time", "-c", "-f",
filename.getAbsolutePath() };
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
Log.d("mCare", "Unable to log...", e);
e.printStackTrace();
}
You can read more here: How to redirect my log output from logcat to the SD-Card on an android device?
Have you tried Ti.API.log?

Categories

Resources