導航:首頁 > 操作系統 > libusbandroid

libusbandroid

發布時間:2024-05-14 12:42:56

⑴ 如何在android手機上使用hackrf

JNI方面
1)將編譯好的庫導入工程
拷貝libhackrf.h、libhackrf.so和libusb1.0.so到工程目錄,創建libusb1.0.mk和libhackrf.mk
libusb1.0.mk
通過libhackrf.mk即可將libhackrf連接到工程中。
2)初始化hackrf,注冊接收函數。
JNI介面
[cpp] view plain
JNIEXPORT jstring JNICALL java_com_pagekpang_hackrftouch_HackRFTouch_test(
JNIEnv *env, jclass cls) {
int result = HACKRF_SUCCESS;
result = hackrf_init();
if (result == HACKRF_SUCCESS) {
return env->NewStringUTF("OK!");
} else {
return env->NewStringUTF("Error!");
}
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_init(
JNIEnv *env, jobject thiz) {
int result = HACKRF_SUCCESS;
uint8_t board_id = BOARD_ID_INVALID;
char pbuf[2048] = { 0 };
result = hackrf_init();
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_init() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}

result = hackrf_open(&device);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_open() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}

result = hackrf_board_id_read(device, &board_id);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_board_id_read() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
sprintf(pbuf, "Board ID Number: %d (%s)\n", board_id,

hackrf_board_id_name((hackrf_board_id) board_id));
return env->NewStringUTF(pbuf);
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setSampleRateManual(

JNIEnv *env, jobject thiz, jlong freq, jint divi) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_sample_rate_manual(device, freq, divi);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_sample_rate_manual() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setVgaGain(
JNIEnv *env, jobject thiz, jint vga) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_vga_gain(device, vga);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_vga_gain() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setLnaGain(
JNIEnv *env, jobject thiz, jint lna) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_lna_gain(device, lna);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_lna_gain() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setFreq(
JNIEnv *env, jobject thiz, jlong freq) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_freq(device, freq);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_freq() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setAmpEnable(

JNIEnv *env, jobject thiz, jint b) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_amp_enable(device, b == 0 ? false : true);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_amp_enable() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}

JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_startRX(
JNIEnv *env, jobject thiz) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_start_rx(device, hackrf_rx_cb, env->NewGlobalRef(thiz));
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_start_rx() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_stopRX(

JNIEnv *env, jobject thiz) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_stop_rx(device);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_stop_rx() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
java層與JNI介面的綁定

[java] view plain
package com.pagekpang.hackrftouch;

public class HackRFTouch {

static {
System.loadLibrary("usb1.0");
System.loadLibrary("hackrf");
System.loadLibrary("HackrfTouch");
}

public static native String test();

private native String init();

private native String setSampleRateManual(long freq, int divi);

private native String setVgaGain(int g);

private native String setLnaGain(int g);

private native String setFreq(long freq);

private native String setAmpEnable(int f);

public native String startRX();

public native String stopRX();

public native float[] readRx();

private String retString = "";
private Boolean isOpen = false;

private ReadRxThread mReadRxThread = null;

public HackRFTouch() {
// TODO Auto-generated constructor stub
retString = init();
if (!retString.contains("failed")) {
isOpen = true;
mReadRxThread = new ReadRxThread(this);
}
}

class ReadRxThread extends Thread {
HackRFTouch mThisHackRFTouch = null;

public ReadRxThread(HackRFTouch t) {
// TODO Auto-generated constructor stub
mThisHackRFTouch = t;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(true){
mThisHackRFTouch.cb(mThisHackRFTouch.readRx());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public Boolean isOpen() {
return isOpen;
}

public String getLastError() {
return retString;
}

public Boolean HsetSampleRateManual(long freq, int divi) {
if (!isOpen) {
return false;
}
retString = setSampleRateManual(freq, divi);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HsetLnaGain(int g) {
if (!isOpen) {
return false;
}
retString = setLnaGain(g);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HsetVgaGain(int g) {
if (!isOpen) {
return false;
}
retString = setVgaGain(g);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HsetFreq(long freq) {
if (!isOpen) {
return false;
}
retString = setFreq(freq);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HstopRX() {
if (!isOpen) {
return false;
}
retString = stopRX();
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

3)FFT運算

[cpp] view plain
if (device != NULL) {
float raw[1024];
int dalen = transfer->valid_length;
float realF, imagF, maxF;
uint8_t *pbuf = transfer->buffer;
while (dalen > 0 && runcount == 0) {
complex<double>* fdata = new complex<double> [1024];
complex<double>* fout = new complex<double> [1024];
for (int i = 0; i < 2048; i += 2) {
fdata[i / 2] = complex<double>(meanN(&pbuf[i], 100), meanN(&pbuf[i + 1], 100));
}
FFT(fdata, fout, 10);
//dft(fdata, 10, 0);
//fout = fdata;
maxF = 0.0;
for (int i = 0; i < 1024; i++) {
raw[i] = pow(pow(fout[i].real(), 2) + pow(fout[i].imag(), 2),
0.5);
if (maxF < raw[i]) {
maxF = raw[i];
}
}

for (int i = 0; i < 1024; i++) {
raw[i] = raw[i] / maxF;
}

sendBuf(raw);
//send(g_client, (char *)&raw, 4*1024, 0); //發送數據
dalen -= 2048;
pbuf += 2048;
runcount = 2;
}
runcount--;
//printf("E");
} else {
printf("O");
}

閱讀全文

與libusbandroid相關的資料

熱點內容
華為通話加密電話 瀏覽:58
什麼伺服器可以代替計算機 瀏覽:960
伺服器當電腦用怎麼安裝顯卡 瀏覽:519
回女友消息神器python 瀏覽:50
App分類框怎麼添加 瀏覽:543
海爾帥王子冰箱壓縮機啟動跳閘 瀏覽:479
cadfill命令什麼意思 瀏覽:334
中公專項題庫pdf 瀏覽:931
拼多多緩存在哪個文件夾 瀏覽:640
安卓無卡汽車導航怎麼升級 瀏覽:48
cs管理員命令是什麼 瀏覽:126
華為v10模擬加密門禁卡 瀏覽:725
android修改app名稱 瀏覽:640
輻射3夥伴命令 瀏覽:247
蘋果電腦pdf轉換成word 瀏覽:921
純小數運演算法則 瀏覽:385
php什麼是事務 瀏覽:859
怎麼添加網名文件夾 瀏覽:769
啊哈演算法語言 瀏覽:677
馬來西亞加密貨幣政策 瀏覽:233