Android如何通过App来控制来呀开发板上Led灯的亮灭
时间: 2024-02-17 20:01:47 浏览: 167
开发板控制灯亮灭
Android通过App来控制开发板上LED灯的亮灭需要使用Android开发板上的GPIO接口。具体步骤如下:
1. 在Android App中添加权限:
在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.SET_TIME_ZONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.WRITE_PROFILE" />
<uses-permission android:name="android.permission.READ_SOCIAL_STREAM" />
<uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" />
<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
<uses-permission android:name="android.permission.ACCESS_ALL_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MTP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
```
2. 在App中创建GPIO控制类:
创建一个名为GPIOControl的Java类,用于控制GPIO口的开关。代码如下:
```java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class GPIOControl {
private static final String GPIO_ROOT_PATH = "/sys/class/gpio/";
private static final String GPIO_EXPORT_PATH = GPIO_ROOT_PATH + "export";
private static final String GPIO_UNEXPORT_PATH = GPIO_ROOT_PATH + "unexport";
private String mGpioPath;
public GPIOControl(int gpio_num) {
mGpioPath = GPIO_ROOT_PATH + "gpio" + gpio_num + "/";
}
public boolean open() {
File gpioFile = new File(GPIO_EXPORT_PATH);
if (!gpioFile.exists()) {
return false;
}
try {
FileWriter exportFile = new FileWriter(gpioFile);
exportFile.write(Integer.toString(getGpioNum(mGpioPath)));
exportFile.flush();
exportFile.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean close() {
File gpioFile = new File(GPIO_UNEXPORT_PATH);
if (!gpioFile.exists()) {
return false;
}
try {
FileWriter unexportFile = new FileWriter(gpioFile);
unexportFile.write(Integer.toString(getGpioNum(mGpioPath)));
unexportFile.flush();
unexportFile.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean setDirection(String direction) {
File directionFile = new File(mGpioPath + "direction");
if (!directionFile.exists()) {
return false;
}
try {
FileWriter directionWriter = new FileWriter(directionFile);
directionWriter.write(direction);
directionWriter.flush();
directionWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean setValue(int value) {
File valueFile = new File(mGpioPath + "value");
if (!valueFile.exists()) {
return false;
}
try {
FileWriter valueWriter = new FileWriter(valueFile);
valueWriter.write(Integer.toString(value));
valueWriter.flush();
valueWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private int getGpioNum(String gpio_path) {
int start = gpio_path.indexOf("gpio");
int end = gpio_path.indexOf("/", start);
String gpio_num_str = gpio_path.substring(start + 4, end);
return Integer.parseInt(gpio_num_str);
}
}
```
3. 在App中调用控制类:
在App的主Activity中调用GPIOControl类,控制LED灯的亮灭。代码如下:
```java
public class MainActivity extends AppCompatActivity {
GPIOControl gpioControl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpioControl = new GPIOControl(23);
gpioControl.open();
gpioControl.setDirection("out");
}
@Override
protected void onDestroy() {
gpioControl.close();
super.onDestroy();
}
public void turnOn(View view) {
gpioControl.setValue(1);
}
public void turnOff(View view) {
gpioControl.setValue(0);
}
}
```
在App的布局文件中添加两个按钮,分别用于控制LED灯的开和关:
```xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/button_turn_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:onClick="turnOn"/>
<Button
android:id="@+id/button_turn_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"
android:onClick="turnOff"/>
</LinearLayout>
```
以上就是使用Android App控制开发板上LED灯的亮灭的具体步骤。
阅读全文