文档详情

获得root权限和蓝牙设置

宝路
实名认证
店铺
DOC
22.50KB
约4页
文档ID:5879736
获得root权限和蓝牙设置_第1页
1/4

任务要求:root 权限源代码和蓝牙设置文件管理器一般在没有 root 的,使用以下下权限即可对 SD 卡的文件进行操作,对的非系统文件夹进行操作,对 root 过的,文件管理管理器可以申请更高的权限(获得 root 权限) ,对的系统文件夹进行操作,比如 system、data 等系统文件夹Android 应用程序获取 ROOT 权限代码 方法一:import java.io.DataOutputStream;import android.app.Activity;public class MyTestPri extends Activity{public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//* Create a TextView and set its content.//* the text is retrieved by calling a native//* function.setContentView(R.layout.main);String apkRoot="chmod 777 /dev/block/mmcblk0";//SD 卡分区路径,也可能是mmcblk1 随系统版本定,当前程序路径请用 getPackageCodePath();RootCmd(apkRoot);}public boolean RootCmd(String cmd){Process process = null;DataOutputStream os = null;try{process = Runtime.getRuntime().exec("su");os = new DataOutputStream(process.getOutputStream());os.writeBytes(cmd+ "\n");os.writeBytes("exit\n");os.flush();process.waitFor();} catch (Exception e) {return false;} finally { try {if (os != null) {os.close();}process.destroy();} catch (Exception e) {}}return true;}}方法二:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class LinuxShell {public static boolean isRoot(Runtime r, long wait)throws IOException, InterruptedException {boolean root = false;Process p = null;BufferedReader errReader = null;p = Runtime.getRuntime().exec("su");Thread.sleep(wait);errReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));if (!errReader.ready()) { root = true;p.destroy();}return root;}}Bluetooth API 的主要方法BluetoothAdapter 类BluetoothAdapter.getDefaultAdapter() :得到本地默认的 BluetoothAdapter ,若返回为 null 则表示本地不支持蓝牙;isDiscovering() :返回设备是否正在发现周围蓝牙设备;cancelDiscovery() :取消正在发现远程蓝牙设备的过程;startDiscovery() :开始发现过程;getScanMode() :得到本地蓝牙设备的 Scan Mode ;getBondedDevices() :得到已配对的设备;isEnabled() :蓝牙功能是否启用。

当发现蓝牙功能未启用时,如下调用设置启用蓝牙:if (! mBluetoothAdapter .isEnabled()) {Intent enableIntent = new Intent(BluetoothAdapter. ACTION_REQUEST_ENABLE );startActivityForResult(enableIntent, REQUEST_ENABLE_BT );}如果发现当前设备没有打开对外可见模式,则传递 Intent 来调用打开可发现模式,代码如下:Intent discoverableIntent = new Intent(BluetoothAdapter. ACTION_REQUEST_DISCOVERABLE ); discoverableIntent.putExtra(BluetoothAdapter. EXTRA_DISCOVERABLE_DURATION , 300);startActivity(discoverableIntent);BluetoothDevice 类,此为对应的远程蓝牙 DevicecreateRfcommSocketToServiceRecord() :创建该 Device 的 socket 。

BluetoothSocket 类connect() :请求连接蓝牙getInputStream() :得到输入流,用于接收远程方信息getOutputStream() :得到输出流,发送给远程方的信息close() :关闭蓝牙连接InputStream 类:read(byte[]) :以阻塞方式读取输入流OutputStream 类:write(byte[]) :将信息写入该输出流,发送给远程具体代码可参考开发者参考文档中的 BluetoothChat可点击以下链接:。

下载提示
相似文档
正为您匹配相似的精品文档