long uuidHigh = new BigInteger("11bc4f7154ac9edb", 16).longValue();
long uuidLow = new BigInteger("167bf05f9436bc0a", 16).longValue();
UUID uuid = new UUID(uuidHigh, uuidLow);
BluetoothSocket socket = null;
BluetoothAdapter blueAdapter = null;
blueAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = blueAdapter.getRemoteDevice("00:07:AB:6A:96:7D");
try {
socket = device.createRfcommSocketToServiceRecord(uuid); // CRASH!
} catch (IOException e) { e.printStackTrace(); }
Result:
E/AndroidRuntime(14079): java.lang.IllegalStateException: Could not execute method of the activity
Statements just before the "socket =" statement in the same method execute fine. Getting the device seems to work fine with getName() working on it.
The physical device it runs on has API 8.
The bluetooth dev guide cryptically says:
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
But createRfcommSocketToServiceRecord() cannot take a String, it must take a UUID.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="intrax.demo.explore"
android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" >
<activity
android:name=".BluetoothAct" android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Related
I run my application in two different Android devices. The first is an Arnova 9 G2 Tablet and the second a Sony Xperia P. In the Arnova tablet my application executed successfully, but in the Sony Xperia P phone, the LogCat displays me the following error:
/data/data/com.example.androidjnetpcaptest/interfaces: open failed:
EACCES (Permission denied) FATAL EXCEPTION: main
java.lang.NullPointerException at
com.example.androidjnetpcaptest.InterfacesFragment.onCreateView(InterfacesFragment.java:35)
My manifest XML file is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidjnetpcaptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.pemission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.Default" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".InformationsActivity"
android:theme="#android:style/Theme.Holo"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.Default" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And the InterfacesFragment class is:
public class InterfacesFragment extends Fragment
{
private Scanner inputStream = null;
private TextView interfacesTextView = null;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
try
{
inputStream = new Scanner( new FileInputStream( "/data/data/com.example.androidjnetpcaptest/interfaces" ) );
}
catch ( FileNotFoundException e )
{
Log.e( "InterfacesFragment_ERROR: ", e.getMessage() );
}
String lines = "";
while ( inputStream.hasNextLine() )
{
lines = lines + inputStream.nextLine() + "\n";
}
inputStream.close();
final ProgressDialog ringProgressDialog = ProgressDialog.show( getActivity(), "Please wait ...", "Finding interfaces ...", true );
ringProgressDialog.setCancelable( true );
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
Thread.sleep( 4000 );
}
catch ( Exception e )
{
}
ringProgressDialog.dismiss();
}
}).start();
View rootView = inflater.inflate(R.layout.interfaces, container, false);
interfacesTextView = ( TextView ) rootView.findViewById( R.id.interfacesTextView );
interfacesTextView.setText( lines );
interfacesTextView.setMovementMethod( new ScrollingMovementMethod() );
return rootView;
}
}
The trouble is the use of the absolute path /data/data/com.example. androidjnetpcaptest/interfaces. You should not assume this is the data location for the app. Apps can be moved to external storage and since ICS the system supports multiple users (people) where the app gets different storage per user. Instead, use Context.getFilesDir() to get the app-private location where files can be created and stored.
You do not need any special permissions to read/write this space.
We are putting together an Unity app that uses Amazon S3 and SNS SDKs. On their own they're working fine - with SNS only the app registers and receives notifications properly, with S3 we can download images and videos fine.
Combining the SDKs into one app is now causing problems and after hours I have found the line that's causing the crash:
GCM.cs
string regId = cls.CallStatic<string>("register",senderIds);
What's odd is that ADB logcat is giving no errors at this point but after putting a return before then after this call it is known this is where the app crashes. Strangely the push registration process continues in ADB while the "This app has crashed" message is being displayed up until it completes with success as if the app starts up properly...
The "register" function being called is in AWSUnityGCMWrapper.java
public static String register(final String senderIds) {
try {
if (senderIds == null) {
return "";
}
Activity activity = UnityPlayer.currentActivity;
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(activity);
String regId = gcm.register(senderIds);
storeRegistrationId(activity, regId);
return regId;
} catch (IOException e) {
Log.e(TAG, "failed to register the to gcm");
Log.e(TAG, "exception = " + e.getMessage());
}
return "";
}
Tried many configs for the AndroidManifest.xml with no luck, except that all seems to be working fine through logcat! The CallStatic returns properly and the process continues nonetheless until 'ok' is pressed on the crash popup...
Any idea why this call static function is causing this problem? Ask for more code samples if needed!
Edit: AndroidManifest.xml if it helps...
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.appname"
android:installLocation="preferExternal"
android:theme="#android:style/Theme.NoTitleBar"
android:versionCode="1"
android:versionName="1.0">
<uses-feature android:name="android.hardware.camera" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- PUSH --><uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- PUSH --><permission android:name="com.company.appname.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<!-- PUSH --><uses-permission android:name="com.company.appname.permission.C2D_MESSAGE" />
<application
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="false">
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity android:name="com.unity3d.player.VideoPlayer"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<!-- PUSH --><receiver
android:name="com.company.appname.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.company.appname" />
</intent-filter>
</receiver>
<!-- PUSH --><service android:name="com.company.appname.GCMIntentService" />
<!--
To support devices using the TI S3D library for stereo mode we must
add the following library.
Devices that require this are: ODG X6
-->
<uses-library android:name="com.ti.s3d" android:required="false" />
<!--
To support the ODG R7 in stereo mode we must add the following library.
-->
<uses-library android:name="com.osterhoutgroup.api.ext" android:required="false" />
</application>
Edit - AWSUnityGCMWrapper.java Using AsyncTask
public static String register(final String senderIds) {
// Async Task
_regID = "0";
_done = false;
new RegTask().execute(senderIds);
while(!_done);
return _regID;
}
public static void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getGCMPreferences(context);
int appVersion = getAppVersion(context);
Log.i(TAG, "Saving regId on app version " + appVersion);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
public static void setReg(String regID) {
_done = true;
_regID = regID;
}
public static String getReg() {
return _regID;
}
private static class RegTask extends AsyncTask<String, Void, String> {
String _regID;
public RegTask() {
super();
_regID = "0";
}
#Override
protected String doInBackground(String... params) {
Log.i(TAG, ">>> Registering With GCM Async Task... Sender: " + params[0]);
String senderIds = params[0];
try {
if (senderIds == null) {
//_regID = "0";
return "0";
}
Log.i(TAG, ">>> Trying Registration...");
Activity activity = UnityPlayer.currentActivity;
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(activity);
String regId = gcm.register(senderIds);
Log.i(TAG, ">>> New Registration ID: " + regId);
storeRegistrationId(activity, regId);
//_regID = regId;
return regId;
} catch (IOException e) {
Log.e(TAG, "failed to register the to gcm");
Log.e(TAG, "exception = " + e.getMessage());
}
//_regID = "0";
Log.i(TAG, ">>> Registering With GCM Async Task Done...");
return "0";
}
#Override
protected void onPostExecute(String result) {
setReg(result);
}
}
In Logcat
I/AWSUnityGCMWrapper(21896): >>> Registering With GCM Async Task... Sender: 750882817708
I/AWSUnityGCMWrapper(23338): >>> New Registration ID: APA91bHWev8CCsbso8JE5a28J...
I/AWSUnityGCMWrapper(21896): Saving regId on app version 1
The app still crashes immediately when the register button is pressed.
So, from our discussion in comments under your post, your code for registering for GCM should be run in UI thread. Namely, you should wrap your function in a runOnUiThread(), which you did by:
Creating a Runnable class
MyRunnable r = new MyRunnable(UnityPlayer.currentActivity, senderIds);
Running it like so:
UnityPlayer.currentActivity.runOnUiThread(r);
What it does is no longer crashing up, but coming up with a exception thrown (which is just called MAIN_THREAD in your logs).
What you should do is instead of just calling .register(...) in your runnable function call it on the background, for example in an AsyncTask:
private void registerBackground(final String senderId) {
new AsyncTask() {
#Override
protected String doInBackground(Void... params) {
String msg = "";
try {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(UnityPlayer.currentActivity);
String regId = gcm.register(senderId);
storeRegistrationId(UnityPlayer.currentActivity, regId);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
#Override
protected void onPostExecute(String msg) {
// msg returned
}
}.execute(null, null, null);
This is because UI operations on android happen on the main thread, but networking has to be done in a background one.
Call this instead of your register function and let me know if that helped.
Edit:
Btw, you might want to listen for changes or wait for them in a proper way since I can see your static f-tion returns the string.
Edit2:
So after my advice you've moved it to a proper async task. But there is another problem of waiting for a result. To do that you can write something like this instead of a while:
Handler myHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
_done = true;
break;
default:
break;
}
}
};
also in your task's onPostExecute() call this
myHandler.sendEmptyMessage(0);
Then, instead of a while() in your function make a runnable or another simple waiter. But for now, you might want to test it with Thread.sleep(1000); inside a while to ease the load on UI.
So I decided to revert back to what I had originally and started stripping down the AndroidManifest.xml (lots of things in there from a colleague which seem to have been redundant). Using the following manifest seems to have solved the problem, there must have been something conflicting...
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.appname"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="com.amazonaws.unity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.amazonaws.unity.permission.C2D_MESSAGE" />
<application
android:theme="#android:style/Theme.NoTitleBar"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<receiver
android:name="com.company.appname.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.company.appname" />
</intent-filter>
</receiver>
<service android:name="com.company.appname.GCMIntentService" />
I will do some tests today and try to find out exactly what part was causing the problem.
I am writing and Android app that would detect and connect to a know embedded device. The issue my side is that it throws exception whenever I am trying to connect:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
try {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getApplicationContext(),"Try Connecting to device : " + device.getName(),
Toast.LENGTH_LONG).show();
byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234");
BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
BlueToothConnect(device);
}catch(Exception e){}
}
}
};
And my BlueToothConnect method is :
public void BlueToothConnect(BluetoothDevice device){
if (deviceName.equals(device.getName()))
{
try
{
//mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mSocket = (BluetoothSocket) m.invoke(device, 1);
mSocket.connect();
GetConnectionDetails();
result = device;
blueTooth_On_off = 1;
}
catch(Exception e){
text.setText(getStackTrace(e));
try{
result = null;
mSocket.close();
blueTooth_On_off = 0;
}
catch(IOException e1){
result = null;
blueTooth_On_off = 0;
}
}
}
}
I am getting below exception error:
java.io.exception:[jsr82] connect: Connection is not created (failed or aborterd)
I cannot do much on embedded device side either.
Below is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.edazzelsmarthome"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.edazzelsmarthome.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".broadcast.PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter>
</receiver>
</application>
</manifest>
please take me out this problem ,I am trying to get file from Ftp server .
here is my code which i am continuously getting exception like
java.net.unknownHostException :ftpbrasnet.no-ip.org.
i have tried in browser its working.
FTPClient client = new FTPClient();
FileOutputStream Foutput = null;
try {
client.connect("ftp://ftpbrasnet.no-ip.org/"); // My Exceptionis here
client.login("win7", "123");
// Create an OutputStream for the file
String filename = "files.txt";
Foutput = new FileOutputStream(filename);
// Fetch file from server
client.retrieveFile("/" + filename, Foutput);
}
catch (IOException e)
{
Log.e("ERROR", e.getStackTrace().toString());
}
finally
{
try
{
if (Foutput != null) {
Foutput.close();
}
client.disconnect();
} catch (IOException e) {
Log.e("ERROR", e.getStackTrace().toString());
}
}
}
and here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Net.estoque"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="#string/app_name" >
<activity android:name="Netestoque"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Have you tried removing the protocol from the URL, like so:
client.connect("ftpbrasnet.no-ip.org/");
I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.
Here is the onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new TwitterDB(this);
String[] columns = new String[1];
columns[0] = TwitterDB.TEXT;
tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0);
tweets = (ListView)findViewById(R.id.listtwitter);
tweets.setAdapter(tAdapter);
Intent intent = new Intent(this, SyncService.class);
intent.putExtra("action", SyncService.TWITTER_SYNC);
this.startService(intent);
}
Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.
public SyncService(){
super("SyncService");
twitterURI = Uri.parse(TWITTER_URI);
Log.i("SyncService", "Constructed");
}
#Override
public void onHandleIntent(Intent i){
int action = i.getIntExtra("action", -1);
Log.i("SyncService", "Retrieved intent");
switch (action){
case TWITTER_SYNC:
try{
url = new URL(twitterString);
conn = (HttpURLConnection)url.openConnection();
syncTwitterDB();
conn.disconnect();
}catch(MalformedURLException e){
Log.w("SyncService", "Twitter URL is no longer valid.");
e.printStackTrace();
}catch(IOException e){
Log.w("SyncService", "Twitter connection could not be established.");
e.printStackTrace();
}
break;
default:;
// invalid request
}
}
And here is the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name="SyncService"/>
</activity>
</application>
</manifest>
Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>
</application>
</manifest>
You need to define the path to the service in your manifest, simply putting the name is not sufficient.
Change
<service android:name="SyncService"/>
to
<service android:name=".SyncService"/>
if SyncService is in the root of your package, add respective folder before .SyncService if needed.