想做个蓝牙app, 但是现在 搜索出 蓝牙设备后, 点击设备, 连接不上。
错误的地方觉得主要有:
第一:java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
和
第二:
11-13 05:06:33.836: D/AbsListView(20856): unregisterIRListener() is called
11-13 05:06:33.836: D/AbsListView(20856): unregisterIRListener() is called
对于第一个错误: 在androidmanifest 已经声明了:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
为什么还是会报错呢?
对于第二处 错误log, 应该怎么处理呢? 还有别的地方错误吗?
请会的朋友帮忙回复,先谢谢啦
主要的代码:
文件::mainactivity.java :
protected void onCreate(Bundle savedInstanceState) {
......
ListView newListView = (ListView) findViewById(R.id.new_devices);
newListView.setAdapter(mNewDevices);
newListView.setOnItemClickListener(mDeviceClickListener);
....
}
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?>av,View v,int arg2,long arg3) {
mBluetoothDevice.discoveryBluetooth_Cancel();
String info = ((TextView)v).getText().toString();
String address =info.substring(info.length()-17);
Log.d(TAG,"my_address="+address);
BluetoothDevice device = mBluetoothDevice
.getRemoteBluetoothDevice(address);
Log.d(TAG,"my_Bluetoothdevice device="+device);
mBluetoothConnection.connect(device);
}
};
文件:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private String mSocketType;
public ConnectThread(BluetoothDevice device) {
Log.i(TAG, "yong: public ConnectThread(): device="+device);
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
Log.i(TAG, "yong: public ConnectThread(): tmp="+tmp);
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "yong: BEGIN mConnectThread ");
//setName("ConnectThread" + mSocketType);
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
// Close the socket
Log.i(TAG, "yong: BEGIN mConnectThread :IOException e ");
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() " + mSocketType
+ " socket during connection failure", e2);
}
connectionFailed();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothConnection.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
----------------------------------------------------
log:
出现的log 如下:
11-13 05:06:31.541: V/BluetoothDiscoveryReceiver(8739): Received: android.bluetooth.adapter.action.DISCOVERY_STARTED
11-13 05:06:31.981: D/DisplayPowerController(2997): applyLightSensorMeasurementSEC : mIsLockZone = false
11-13 05:06:32.341: D/DisplayPowerController(2997): applyLightSensorMeasurementSEC : mIsLockZone = true
11-13 05:06:33.796: E/bt-btif(25644): property type:241, len:0 is invalid
11-13 05:06:33.801: V/BluetoothEventManager(8739): Received android.bluetooth.device.action.NAME_CHANGED
11-13 05:06:33.801: E/BluetoothRemoteDevices(25644): devicePropertyChangedCallback: bdDevice: 94:63:D1:F7:DB:7E, value is empty for type: 241
11-13 05:06:33.811: D/BluetoothDevicePreference(8739): Is my device connected::false
11-13 05:06:33.811: I/BluetoothDevicePreference(8739): summaryResId else part
11-13 05:06:33.811: V/BluetoothEventManager(8739): Received android.bluetooth.device.action.CLASS_CHANGED
11-13 05:06:33.816: D/MainActivity(20856): yong: mReceiver: BluetoothDevice.ACTION_FOUND:noDevice= null
11-13 05:06:33.816: D/BluetoothDevicePreference(8739): Is my device connected::false
11-13 05:06:33.816: I/BluetoothDevicePreference(8739): summaryResId else part
11-13 05:06:33.821: V/BluetoothEventManager(8739): Received android.bluetooth.device.action.FOUND
11-13 05:06:33.821: E/CachedBluetoothDevice(8739): calling dispatchAttributesChanged
11-13 05:06:33.826: D/BluetoothDevicePreference(8739): Is my device connected::false
11-13 05:06:33.826: I/BluetoothDevicePreference(8739): summaryResId else part
11-13 05:06:33.836: D/AbsListView(20856): unregisterIRListener() is called
11-13 05:06:33.836: D/AbsListView(20856): unregisterIRListener() is called
11-13 05:06:34.861: D/InputReader(2997): Input event: value=1 when=92647873787000
11-13 05:06:34.861: I/InputReader(2997): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.6002 ] when=92647873946000
11-13 05:06:34.861: I/InputDispatcher(2997): Delivering touch to: action: 0x4
11-13 05:06:34.861: I/InputDispatcher(2997): Delivering touch to: action: 0x0
11-13 05:06:34.986: D/InputReader(2997): Input event: value=0 when=92647997218000
11-13 05:06:34.986: I/InputReader(2997): Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=92647997221000
11-13 05:06:34.986: I/InputDispatcher(2997): Delivering touch to: action: 0x1
11-13 05:06:35.076: E/DatabaseUtils(2997): Writing exception to parcel
11-13 05:06:35.076: E/DatabaseUtils(2997): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
11-13 05:06:35.076: E/DatabaseUtils(2997): at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:14615)
11-13 05:06:35.076: E/DatabaseUtils(2997): at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2258)
11-13 05:06:35.076: E/DatabaseUtils(2997): at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:663)
11-13 05:06:35.076: E/DatabaseUtils(2997): at android.content.ContentProvider$Transport.call(ContentProvider.java:325)
11-13 05:06:35.076: E/DatabaseUtils(2997): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:275)
11-13 05:06:35.076: E/DatabaseUtils(2997): at android.os.Binder.execTransact(Binder.java:404)
11-13 05:06:35.076: E/DatabaseUtils(2997): at dalvik.system.NativeStart.run(Native Method)
11-13 05:06:35.076: D/MainActivity(20856): my_address=94:63:D1:F7:DB:7E
11-13 05:06:35.076: D/MainActivity(20856): my_Bluetoothdevice device=94:63:D1:F7:DB:7E
11-13 05:06:35.076: D/BluetoothConnect(20856): yong: connect to 94:63:D1:F7:DB:7E
11-13 05:06:35.076: I/BluetoothConnect(20856): yong: public ConnectThread(): device=94:63:D1:F7:DB:7E
11-13 05:06:35.076: W/ActivityManager(2997): Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
------解决思路----------------------
debug到哪句话会报错误
------解决思路----------------------
第一:
Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0
引用自 http://www.tuicool.com/articles/Q7RjAr
log中直接给出提示,需要加一个权限INTERACT_ACROSS_USERS_FULL,这个权限时API17新引入的,目的在于允许不同用户的应用之间可以产生交互。可是加上去之后发现,还不是无法调起浏览器,而且log依然提示需要权限INTERACT_ACROSS_USERS_FULL,很是奇怪,于是继续分析。
首先说明一下Linux中的pid和uid,以及android扩展的userSerialNumber。pid是Process的标识,用于系统对进程的控制,从API层面看就是用于Process.killProcess()和Process.sendSignal();uid在Linux系统中是用来标识用户的,而在android将uid视为app的标识id,用于"sandbox"安全模型,即用于app权限控制;而对于API17引入的多用户支持(目前只支持平板),uid已经被占用,只好新引入userSerialNumber来标识用户。
回到刚才的问题,log中告知startActivity时运行用户标识为-2,而调用却是由用户标识0发起,导致拒绝执行。用这句话搜索,发现在Google开发者网站中有相关的issue,链接如下: https://code.google.com/p/android/issues/detail?id=39801 (打不开可以把https改为http)。结合官方的回答,问题原因如下:由于被卸载,C端进程监听到目录被删除,立即执行am命令,此时将会默认以USER_CURRENT的身份执行,由于API17中ActivityManagerService.handleIncomingUser()会校验userSerialNumber,发现用户标识不匹配,导致权限校验失败——这也说明了权限的影响范围仅限于Java端的进程,对于fork()出来的C端进程来说,并不继承父进程在Android中声明的权限。
解决方案:增加处理分支,若API>=17,将userSerialNumber传递给C端进程,然后在am命令中带上参数--user userSerialNumber即可。
问题2:
八成是控件用法的有问题。最好多贴一下 newListView 相关的代码。
------解决思路----------------------
tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
目测这个uuid 有问题吧。
我没搞过蓝牙相关的东西,不了具体怎么改, 楼主参考之前的链接看看人家怎么处理,再改呗。
问题二,我去查了一下android 4.4 和5.L源码,貌似都没有 unregisterIRListener 这个LOG相关的代码。
要是源码改过的话,具体代表啥意思就不知道了。
看样子这个仅仅只是输出debug的信息而已,对APP没影响吧。
另外你的代码,我没看出有啥问题。